Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the hover button background color of a website using selenium?

Hello I am using python selenium to get some css propertiew of a button. I need also the hover background color. The css is like this:

.overview .add-to-cart-button:hover, .variant-overview .add-to-cart-button:hover {
background-color: #b41733;}

My code is this:

    from selenium import webdriver 
from selenium.webdriver.support.color import Color
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome(executable_path = ChromeDriverManager().install())
driver.get('https://www.inshoes.gr/andrikes-zones-andrikes-zones-dermatines-diplis-opseos-33000481-kamel-mavro')


background_color = driver.find_element_by_id('addtocartbutton-7531').value_of_css_property('background-color')
text_color = driver.find_element_by_id('addtocartbutton-7531').value_of_css_property('color')
hex_color = Color.from_string(background_color).hex
hex_text_color = Color.from_string(text_color).hex
print(hex_color)
print(hex_text_color)

Can someone help me?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To retrieve the hover button background color of the WebElement using Selenium first you need to Mouse Hover inducing WebDriverWait for the visibility_of_element_located() and then use value_of_css_property() and you can use the following Locator Strategies:

Code Block:

driver = webdriver.Chrome(service=s, options=options)
driver.get("https://www.inshoes.gr/andrikes-zones-andrikes-zones-dermatines-diplis-opseos-33000481-kamel-mavro")
basket = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#addtocartbutton-7531")))
ActionChains(driver).move_to_element(basket).click().perform()
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#addtocartbutton-7531"))).value_of_css_property('background-color'))

Console Output:

rgba(242, 35, 65, 1)

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading