如何解决循环的selenium中的此错误?InvalidSelectorException:消息:无效选择器:找不到具有



我曾尝试在selenium中为网站创建一个for循环,以检查几个按钮中的一个是否可点击,然后点击它,如果不刷新它,直到它可点击为止。现在,使用我的xpath,我在以下代码中得到错误(InvalidSelectorException:消息:无效选择器:无法找到具有xpath表达式xpath=…的元素

如果你能帮忙就太好了。我还没有找到任何能帮助我的东西!否则,为我的问题找到一个不同的解决方案也会很好!非常感谢。

paths = ['Xpath=//*[@id="mms-app-root"]/div[2]/div/div[2]/div[2]/div[2]/div/div/div/div/div[4]/div[1]/div[2]/button', 'Xpath=//*[@id="mms-app-root"]/div[2]/div/div[2]/div[2]/div[3]/div/div/div/div/div[4]/div[1]/div[2]/button', 'Xpath=//*[@id="mms-app-root"]/div[2]/div/div[2]/div[2]/div[4]/div/div/div/div/div[4]/div[1]/div[2]/button']
for i in range(len(paths)):
if(len(wd.find_elements_by_xpath(paths[i])) > 0):
wd.find_element_by_xpath(paths[i]).click()
break
else:                                                                                                          
print('not available')
time.sleep(8)
wd.refresh()
time.sleep(2)
InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression Xpath=//*[@id="mms-app-root"]/div[2]/div/div[2]/div[2]/div[2]/div/div/div/div/div[4]/div[1]/div[2]/button because of the following error:
TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted to the desired type.

似乎需要从表达式中删除Xpath=前缀才能使其有效。

最新更新