下面的代码从浏览器的无序列表中找到正确的链接,但是该EC.element_to_be_clickable
功能不起作用,因为如果链接不可点击,它将需要刷新浏览器(再次检查)。
相反,是否有任何方法可以检查链接是否被禁用(如果不是,则单击()?该链接将采用以下格式之一
<a class="Button disabled">Purchase</a>
<a class="Button">Purchase</a>
下面的代码
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
try:
if len(driver.find_elements(By.XPATH, "//span[text()='$30.00']/../following-sibling::div/a[text()='Purchase']")) > 0:
print("Found, now attempting to click link")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='$30.00']/../following-sibling::div/a[text()='Purchase']"))).click()
检查链接是否被禁用,即。如果它的类包含“禁用”,只需在它的类中查找它:
try:
if len(driver.find_elements(By.XPATH, "//span[text()='$30.00']/../following-sibling::div/a[text()='Purchase']")) > 0:
elem = driver.find_element(By.XPATH, "//span[text()='$30.00']/../following-sibling::div/a[text()='Purchase']")
print("Found, now attempting to click link")
if "disabled" in elem.get_attribute("class"):
print("Link disabled! Refreshing page.")
driver.refresh()
else:
elem.click()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句