简体   繁体   English

无法到达WebElement

[英]Can't reach WebElement

I'm getting an error the first time I try to get an element. 第一次尝试获取元素时出现错误。 Notice that second time I try that it actually works. 请注意,第二次我尝试它确实有效。 This is a debug session: 这是一个调试会话:

>>> 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>

And this is the code: 这是代码:

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)

Found this comment on /usr/lib/python3.4/http/client.py:319: 在/usr/lib/python3.4/http/client.py:319上找到以下注释:

Presumably, the server closed the connection before sending a valid response. 大概是服务器在发送有效响应之前关闭了连接。

Additional info: 附加信息:

  • OS: Ubuntu 14.04.2 LTS 作业系统:Ubuntu 14.04.2 LTS
  • Python: 3.4.0 的Python:3.4.0
  • Django: 1.7.6 的Django:1.7.6
  • selenium: 2.45.0 硒:2.45.0
  • Firefox: 36.0.4 的Firefox:36.0.4

Any idea what could I be missing? 知道我会丢失什么吗?

As said when something goes strange with Selenium it is usually good idea to update it and also update firefox. 如前所述,当Selenium出现问题时,通常最好进行更新,同时也更新Firefox。

In your case are you sure that element was actually loaded? 您是否确定确实已加载该元素? I can see implicitly wait method but loading time may vary so it might be not enough. 我可以看到implicitly wait方法,但是加载时间可能会有所不同,因此可能不够。

You can start by increasing implicitly_wait time and try once again. 您可以从增加implicitly_wait时间开始,然后重试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM