【问题标题】:Can't reach WebElement无法访问 WebElement
【发布时间】:2015-03-31 08:41:19
【问题描述】:

我第一次尝试获取元素时遇到错误。请注意,我第二次尝试它确实有效。这是一个调试会话:

>>> self.selenium.find_element_by_xpath('//*[@id="add-button"]')
Traceback (most recent call last):
  File "/home/vagrant/.pycharm_helpers/pydev/pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
  File "/vagrant/venv/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 232, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/vagrant/venv/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 664, in find_element
    {'using': by, 'value': value})['value']
  File "/vagrant/venv/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 173, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/vagrant/venv/lib/python3.4/site-packages/selenium/webdriver/remote/remote_connection.py", line 349, in execute
    return self._request(command_info[0], url, body=data)
  File "/vagrant/venv/lib/python3.4/site-packages/selenium/webdriver/remote/remote_connection.py", line 380, in _request
    resp = self._conn.getresponse()
  File "/usr/lib/python3.4/http/client.py", line 1147, in getresponse
    response.begin()
  File "/usr/lib/python3.4/http/client.py", line 351, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.4/http/client.py", line 321, in _read_status
    raise BadStatusLine(line)
http.client.BadStatusLine: ''
>>> self.selenium.find_element_by_xpath('//*[@id="add-button"]')
<selenium.webdriver.remote.webelement.WebElement object at 0x7f90ab074400>

这是代码:

from selenium.webdriver.firefox.webdriver import WebDriver
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from xvfbwrapper import Xvfb

class UIPropertyTestCase(StaticLiveServerTestCase):
    fixtures = ['properties']

    @classmethod
    def setUpClass(cls):
        cls.vdisplay = Xvfb()
        cls.vdisplay.start()
        cls.selenium = WebDriver()
        cls.selenium.implicitly_wait(10)
        super(UIPropertyTestCase, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        cls.selenium.quit()
        cls.vdisplay.stop()
        super(UIPropertyTestCase, cls).tearDownClass()

    def test_add_property(self):
        self.selenium.get('{0}{1}'.format(self.live_server_url, '/#/app/properties'))
        self.selenium.find_element_by_xpath('//*[@id="add-button"]').click()

        self.selenium.get('{0}{1}'.format(self.live_server_url, '/#/app/properties'))
        count = len(self.selenium.find_elements_by_xpath('//*[@id="data"]/tbody/tr'))
        self.assertEqual(count, 3)

在 /usr/lib/python3.4/http/client.py:319 上找到这条评论:

大概是服务器在发送一个有效的之前关闭了连接 回应。

附加信息:

  • 操作系统:Ubuntu 14.04.2 LTS
  • Python:3.4.0
  • Django:1.7.6
  • 硒:2.45.0
  • 火狐:36.0.4

知道我会错过什么吗?

【问题讨论】:

  • 将定位器包装在 range(3) 的循环中似乎可行,但这是一个非常讨厌的解决方案:(

标签: python django selenium xvfb


【解决方案1】:

如前所述,当 Selenium 出现异常时,更新它并更新 firefox 通常是个好主意。

在您的情况下,您确定该元素已实际加载吗?我可以看到implicitly wait 方法,但加载时间可能会有所不同,因此可能还不够。

您可以先增加implicitly_wait 的时间,然后再试一次。

【讨论】:

  • 这很奇怪。在 implicitly_wait 调用上更改值时,我看到了奇怪的行为。有时它只是抛出我在上面粘贴的错误,有时它只是挂起:/
  • @micgeronimo,是的。一切都是最新的:/
猜你喜欢
  • 2016-02-18
  • 1970-01-01
  • 1970-01-01
  • 2020-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多