尝试在 Selenium Python 中使用 WebDriverWait 直到 presence_of_element_located 而不是 sleep.time
Trying to use WebDriverWait until presence_of_element_located instead of sleep.time in Selenim Python
我有这段代码可以按原样工作:
time.sleep(0.7)
self.driver.find_element_by_xpath("anyXpath").click()
我让程序等待一段时间,所以弹出窗口 window 上的按钮确实可以点击,这个解决方案有效。
现在,我想做同样的事情,但是 WEbDriverWait
我写了这个:
WebDriverWait(self.driver,10).until(expected_conditions.presence_of_element_located(By.XPATH, "anyXpath"))
self.driver.find_element_by_xpath("anyXpath").click()
但是按钮从未被点击过。我究竟做错了什么?我愿意使用不同于 presence_of_element_located 的另一个预期条件,但我想它也应该适用于后者。
谢谢。
你可以试试这个方法。它等待元素可以点击
WebDriverWait(10, driver).until(EC.element_to_be_clickable(By.XPATH, "anyXpath"))
进口
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
通过导入 WebDriverWait
、expected_conditions
和 By
,您可以等到 WebDriver 认为元素可点击后再点击该元素。
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait
WebDriverWait(10, self.driver).until(
expected_conditions.element_to_be_clickable(
(By.XPATH, "//xpath//to//element")
)
).click()
确保将元组传递给 element_to_be_clickable()
。
我有这段代码可以按原样工作:
time.sleep(0.7)
self.driver.find_element_by_xpath("anyXpath").click()
我让程序等待一段时间,所以弹出窗口 window 上的按钮确实可以点击,这个解决方案有效。
现在,我想做同样的事情,但是 WEbDriverWait
我写了这个:
WebDriverWait(self.driver,10).until(expected_conditions.presence_of_element_located(By.XPATH, "anyXpath"))
self.driver.find_element_by_xpath("anyXpath").click()
但是按钮从未被点击过。我究竟做错了什么?我愿意使用不同于 presence_of_element_located 的另一个预期条件,但我想它也应该适用于后者。 谢谢。
你可以试试这个方法。它等待元素可以点击
WebDriverWait(10, driver).until(EC.element_to_be_clickable(By.XPATH, "anyXpath"))
进口
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
通过导入 WebDriverWait
、expected_conditions
和 By
,您可以等到 WebDriver 认为元素可点击后再点击该元素。
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait
WebDriverWait(10, self.driver).until(
expected_conditions.element_to_be_clickable(
(By.XPATH, "//xpath//to//element")
)
).click()
确保将元组传递给 element_to_be_clickable()
。