Python,selenium-如何刷新页面直到找到项目?

Python,selenium-如何刷新页面直到找到项目?,python,html,selenium,refresh,zero-width-space,Python,Html,Selenium,Refresh,Zero Width Space,我正在尝试重新构建页面,直到项目出现,但我的代码不起作用(我采用了该模式:) 代码如下: while True: try: for h1 in driver.find_elements_by_class_name("name-link"): text = h1.text.replace('\uFEFF', "") if "Puffy" in text: break except NoS

我正在尝试重新构建页面,直到项目出现,但我的代码不起作用(我采用了该模式:)

代码如下:

while True:
    try:
        for h1 in driver.find_elements_by_class_name("name-link"):
            text = h1.text.replace('\uFEFF', "")
            if "Puffy" in text:
                break
    except NoSuchElementException:
        driver.refresh
    else:
        for h1 in driver.find_elements_by_class_name("name-link"):
            text = h1.text.replace('\uFEFF', "")
            if "Puffy" in text:
                h1.click()
                break
        break
这些片段是因为我必须找到一个具有相同类名的项目,并将BOM替换为“”(
find\u element\u by\u partial\u link\u text
不起作用)


有人能帮我吗?非常感谢。

您正在尝试获取元素列表(
driver.find\u elements\u by\u class\u name()
可能会返回元素列表或空列表-无异常)-在这种情况下,您无法获取
NoTouchElementException
,因此将不会执行
driver.refresh
。试试下面

while True:
    if any(["Puffy" in h1.text.replace('\uFEFF', "") for h1 in driver.find_elements_by_class_name("name-link")]):
        break
    else:
        driver.refresh
driver.find_element_by_xpath("//*[contains(., 'Puffy')]").click()

但是我用哪种方式可以点击这些元素呢?哦,我没有注意到你也想要点击它。。。请尝试
driver。通过\u xpath(“/*[包含(,'poffy')]”)查找\u元素。单击()
但是,我不需要这个(单击),但您的答案是有效的。谢谢为什么页面上没有这些项目?为什么刷新会让它们出现?
while True:
    if any(["Puffy" in h1.text.replace('\uFEFF', "") for h1 in driver.find_elements_by_class_name("name-link")]):
        break
    else:
        driver.refresh
driver.find_element_by_xpath("//*[contains(., 'Puffy')]").click()