您好我是使用selenium webdriver的新手,我正在尝试使用python中的selenium webdriver创建一个测试脚本,我正在尝试检查页面中是否存在某个文本字段。
以下是我创建的用于检查文本字段是空白还是空的代码:
driver = self.driver
# Get page by URL
driver.get(self.base_url + "/userk/mission/")
# Get page by URL
driver.get(self.base_url + "/mission/update/123-456-789/")
self.assertEqual(self.base_url + "/mission/update/123-456-789/", driver.current_url)
driver.find_element_by_id("discussion-btn").click()
# Check if text is not present
self.assertEqual("", driver.find_element_by_xpath("//div[5]/div"))
这是我在运行测试时收到的错误:
Traceback (most recent call last):
File "acttemplate_layouts.py", line 37, in test_task_xml
self.assertEqual("", driver.find_element_by_xpath("//div[5]/div"))
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 290, 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 744, in find_element
{'using': by, 'value': value})['value']
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 231, in execute
response = self.command_executor.execute(driver_command, params)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py", line 395, in execute
return self._request(command_info[0], url, body=data)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py", line 426, in _request
resp = self._conn.getresponse()
File "/usr/lib/python2.7/httplib.py", line 1051, in getresponse
response.begin()
File "/usr/lib/python2.7/httplib.py", line 415, in begin
version, status, reason = self._read_status()
File "/usr/lib/python2.7/httplib.py", line 379, in _read_status
raise BadStatusLine(line)
BadStatusLine: ''
使用此测试脚本会给我一个错误。请帮助我使用正确的语法或方法。提前谢谢。
答案 0 :(得分:2)
在driver.find_element_by_xpath(“// div [5] / div”)
后面添加.textself.assertEqual("", driver.find_element_by_xpath("//div[5]/div").text)
答案 1 :(得分:0)
您可以将两个字符串与Assert.assertEqual
进行比较。
在这里,您要将元素与字符串进行比较,这是不正确的。
您应该将元素的文本存储在字符串中,例如:
element a = driver.find_element_by_xpath("//div[5]/div))
a.text()
现在将此a.text()
存储为字符串,并在Assert.assertEqual
中使用它来比较这两个值。