【问题标题】:How can I solve this error in selenium for a loop? InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath如何解决硒循环中的这个错误? InvalidSelectorException:消息:无效选择器:无法使用 xpath 定位元素
【发布时间】:2022-01-16 10:45:30
【问题描述】:

我试图在 selenium 中为网站创建一个 for 循环,以检查几个按钮之一是否可单击,然后单击它,如果在可单击之前不刷新它。现在使用我的 Xpath,我在以下代码下方收到错误(InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression 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.

【问题讨论】:

    标签: html selenium loops xpath


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-04-05
      • 2018-09-29
      • 1970-01-01
      • 2019-09-19
      • 2018-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-01
      相关资源
      最近更新 更多