如何使用硒获取网站的悬停按钮背景颜色?

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

您好,我正在使用 python selenium 来获取按钮的某些 css 属性。我还需要悬停背景颜色。 css 是这样的:

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

我的代码是这样的:

    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)

有人可以帮助我吗?

检索 using first you need to inducing WebDriverWait for the and then use and you can use the following 的悬停按钮背景颜色:

代码块:

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'))

控制台输出:

rgba(242, 35, 65, 1)

注意:您必须添加以下导入:

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