您好,我正在使用 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)
有人能幫我嗎?
uj5u.com熱心網友回復:
要首先使用Selenium檢索WebElement的懸停按鈕背景顏色,您需要為可見性_of_element_located()引入滑鼠懸停誘導WebDriverWait,然后使用value_of_css_property()并且您可以使用以下定位器策略:
代碼塊:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/391991.html
標籤:Python 硒 硒网络驱动程序 网络驱动程序等待 css 属性值
