[英]Can't reach WebElement
第一次嘗試獲取元素時出現錯誤。 請注意,第二次我嘗試它確實有效。 這是一個調試會話:
>>> 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上找到以下注釋:
大概是服務器在發送有效響應之前關閉了連接。
附加信息:
知道我會丟失什么嗎?
如前所述,當Selenium出現問題時,通常最好進行更新,同時也更新Firefox。
您是否確定確實已加載該元素? 我可以看到implicitly wait
方法,但是加載時間可能會有所不同,因此可能不夠。
您可以從增加implicitly_wait
時間開始,然后重試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.