python xpath似乎不适用于某些元素

时间:2022-11-07 12:17:20

I'm trying out Selenium. I've built a quick script using the Selenium IDE. It logs in to a page, opens up a 'Search' window, does a search, and then opens up one of the results.

我正在尝试Selenium。我使用Selenium IDE构建了一个快速脚本。它登录到页面,打开“搜索”窗口,进行搜索,然后打开其中一个结果。

When I run the test in the Selenium IDE, it works fine. When I export to a Java JUnit 4 test case, it works fine. But when I export to a Python 2 unittest, the xpath searching seems to be very buggy...

当我在Selenium IDE中运行测试时,它运行正常。当我导出到Java JUnit 4测试用例时,它工作正常。但是当我导出到Python 2单元测试时,xpath搜索似乎非常错误......

Example:

The page has an input element with name='keyword'. When I search using java with driver.findElement(By.name("keyword")); it's all good - it finds the element.

该页面有一个名为='keyword'的输入元素。当我使用带有driver.findElement(By.name(“keyword”))的java搜索时;这一切都很好 - 它找到了元素。

When I search using python with browser.find_element_by_name("keyword"), it can't seem to find it. I've also tried browser.find_element_by_xpath("//input[@name='keyword']") and browser.find_element_by_xpath("/html/body/form/div/ul/li[2]/input"), the second of which is the xpath I get when I use firebug and the Copy XPath feature.

当我使用browser.find_element_by_name(“keyword”)使用python进行搜索时,似乎无法找到它。我也尝试过browser.find_element_by_xpath(“// input [@ name ='keyword']”)和browser.find_element_by_xpath(“/ html / body / form / div / ul / li [2] / input”),第二个是我使用firebug和Copy XPath功能时得到的xpath。

I've tried adding a delay to the python code before it searches for the input, but to no avail.

我尝试在搜索输入之前向python代码添加延迟,但无济于事。

Does anyone have any idea why the python calls can't seem to find the element? This seems to happen with different elements throughout the pages of the web app (but doesn't happen in the Selenium IDE that I've found so far)...

有谁知道为什么python调用似乎无法找到元素?这似乎发生在Web应用程序的各个页面中的不同元素(但是到目前为止我发现的Selenium IDE中没有发生)...

I appreciate any help you guys can provide!

我感谢你们提供的任何帮助!

Edit:

The error showing in the console is:

控制台中显示的错误是:

Traceback (most recent call last):
  File "test.py", line 37, in <module>
    elem = browser.find_element_by_xpath("/html/body/form/div/ul/li[2]/input")
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 213, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 671, in find_element
    {'using': by, 'value': value})['value']
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 156, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 147, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"xpath","selector":"/html/body/form/div/ul/li[2]/input"}'

Cheers

Jarrett

1 个解决方案

#1


0  

Turns out, the website was opening a new window when a link was clicked, but the driver was still referencing the old window. Not sure why the Selenium IDE export didn't catch that.

事实证明,当点击链接时,网站正在打开一个新窗口,但驱动程序仍然引用旧窗口。不确定为什么Selenium IDE导出没有捕获到它。

Anyway, I had to set the window for the driver to the newly opened window, and then it worked ok.

无论如何,我必须将驱动程序的窗口设置为新打开的窗口,然后它才能正常工作。

Hope this might help other people.

希望这可以帮助其他人。

Cheers

Jarrett

#1


0  

Turns out, the website was opening a new window when a link was clicked, but the driver was still referencing the old window. Not sure why the Selenium IDE export didn't catch that.

事实证明,当点击链接时,网站正在打开一个新窗口,但驱动程序仍然引用旧窗口。不确定为什么Selenium IDE导出没有捕获到它。

Anyway, I had to set the window for the driver to the newly opened window, and then it worked ok.

无论如何,我必须将驱动程序的窗口设置为新打开的窗口,然后它才能正常工作。

Hope this might help other people.

希望这可以帮助其他人。

Cheers

Jarrett