【问题标题】:Trying to use WebDriverWait until presence_of_element_located instead of sleep.time in Selenim Python尝试在 Selenium Python 中使用 WebDriverWait 直到 present_of_element_located 而不是 sleep.time
【发布时间】:2021-03-14 02:03:25
【问题描述】:

我有这段代码可以按原样工作:

time.sleep(0.7)

self.driver.find_element_by_xpath("anyXpath").click()  

我让程序等待一段时间,因此弹出窗口上的按钮确实可以单击,并且此解决方案有效。 现在,我想做同样的事情,但使用 WEbDriverWait 我写了这个:

WebDriverWait(self.driver,10).until(expected_conditions.presence_of_element_located(By.XPATH, "anyXpath"))

self.driver.find_element_by_xpath("anyXpath").click()    

但该按钮从未被点击过。我究竟做错了什么?我愿意使用与 present_of_element_located 不同的另一个预期条件,但我想它也应该适用于后者。 谢谢。

【问题讨论】:

  • 使用元素可点击,然后在其后使用.click()。

标签: python selenium webdriverwait


【解决方案1】:

通过导入WebDriverWaitexpected_conditionsBy,您可以等到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()

【讨论】:

  • 谢谢!它现在似乎工作了,我交换了“10”和“司机”的位置,并使用“self.driver”而不是“司机”
【解决方案2】:

你可以试试这个方法。它等待元素被点击

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

【讨论】:

  • 它也不起作用。您写了“(10,驱动程序)”,我尝试在括号内的不同位置使用“10”和“驱动程序”/“self.driver”。我还使用了 expected_conditions 而不是 EC,我猜它是一样的。
  • 该部分 element_to_be_clickable 工作吗?它等待 html 元素可点击
  • 如果 [element_to_be_clickable] 不起作用?我认为问题在于弹出窗口。在单击按钮之前,您可能需要切换到该窗口。
  • 问题在于传递给element_to_be_clickable 的值。该函数需要一个元组。也没有导入By
  • 确保导入所有需要的东西。 [element_to_be_clickable] 只需要一个定位器,所以我认为你应该重新检查你是否输入了定位器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-24
  • 1970-01-01
  • 2020-07-18
  • 2022-11-10
  • 2015-09-07
相关资源
最近更新 更多