我正在用网页浏览器在python中创建一个程序。这是一个互联网问题。当互联网运行缓慢时,程序会收到错误(xpath is not found)并停止运行。我还使用了sleep函数
如何创建xpath的while循环?或任何其他方法请解释。
我做过这个..。
browser.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/div[1]/div/span').click()
time.sleep(3)我想做
当网络变慢的时候。程序将等待xpath,然后单击。
发布于 2021-06-23 17:53:57
尝试使用Explicit Wait
请参阅此链接:https://www.selenium.dev/documentation/en/webdriver/waits/#explicit-wait
发布于 2021-06-23 18:03:22
您可以并且应该使用webdriver。
它正是为此目的而提出的。
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/div[1]/div/span'))).click()您还应该使用更好的定位器。
//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/div[1]/div/span
看起来一点都不好。
https://stackoverflow.com/questions/68104882
复制相似问题