python selenium保持清爽,直到找到物品(Chromedriver) [英] python selenium keep refreshing until item found (Chromedriver)
问题描述
我试图通过使用selenium来保持刷新当前chromepage的Python脚本功能,直到例如使
我在想这个:
while not driver.find_element_by_partial_link_text (Schott):
driver.refresh
driver.find_element_by_partial_link_text(Schott)。click()
然而,似乎函数 driver.find_element_by_partial_link_text(Schott)
并不是匹配需求的方式。有没有其他方式可以实现这个请求?
顺便说一句,目前我使用driver.get(url)打开网页,但我想知道如何运行我已经打开的现有网页上的脚本?
使用 find_element _... $ c $如果找不到元素,c>会引发
NoSuchElementExeception
。由于我不知道你在做什么网站,我不知道最佳做法是什么。但是,如果只是刷新页面以检查元素,则可以尝试以下操作:从selenium导入webdriver $ b中的
$ b from selenium.common.exceptions import NoSuchElementException
driver.Firefox()#或任何您正在使用的webdriver
driver.get(您将要访问的网址)
True:
try:
driver.find_element_by_partial_link_text(Schott):
,除了NoSuchElementException:
driver.refresh
else:
driver.find_element_by_partial_link_text(点击()
break
I am trying to achieve the feature of a Python script using selenium to keep refreshing the current chromepage until, for example, the certain item that makes driver.find_element_by_partial_link_text("Schott")
is found.
I was thinking about this:
while not driver.find_element_by_partial_link_text("Schott"):
driver.refresh
driver.find_element_by_partial_link_text("Schott").click()
However, it seems like the function driver.find_element_by_partial_link_text("Schott")
is not the way to match the need. Is there other way I can achieve this please?
BTW, currently I am using driver.get(url) to open the webpage but I am wondering how do i run the script on existing webpage that I already open?
Using find_element_...
will raise a NoSuchElementExeception
if it can't find the element. Since I don't know what site you are running this against, i don't know what the best practice would be. However, if it's simply refreshing the page to check for the element, you could try the following:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
driver.Firefox() # or whatever webdriver you're using
driver.get(url that you are going to)
while True:
try:
driver.find_element_by_partial_link_text("Schott"):
except NoSuchElementException:
driver.refresh
else:
driver.find_element_by_partial_link_text("Schott").click()
break
这篇关于python selenium保持清爽,直到找到物品(Chromedriver)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!