我使用的是一个使用seleniumfirefox驱动程序在Python中生成的简单测试。 我想让Firefox静音,所以我用了幻影。 这段代码在Firefox上运行得很好,但在PhantomJS中却不是这样。在
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class Webdriver(unittest.TestCase):
def setUp(self):
#self.driver = webdriver.Firefox()
self.driver = webdriver.PhantomJS()
self.driver.implicitly_wait(30)
self.base_url = "https://www.google.fr/"
self.verificationErrors = []
self.accept_next_alert = True
def test_webdriver(self):
driver = self.driver
driver.get(self.base_url + "/?gfe_rd=cr&ei=SuqkVv2gJ4_u8wfRjbnIBg&gws_rd=ssl")
driver.find_element_by_id("lst-ib").clear()
driver.find_element_by_id("lst-ib").send_keys("this is a test")
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
这是我的错误:
^{pr2}$编辑
根据@alecxe的响应,向PhantomJS添加一个代理对某些测试有效,但在其他测试中失败。在
错误消息是:
Message:{
"errorMessage":"Element is not currently visible and may not be manipulated",
"request":{
"headers":{
"Accept":"application/json",
"Accept-Encoding":"identity",
"Connection":"close",
"Content-Length":"81",
"Content-Type":"application/json;charset=UTF-8",
"Host":"127.0.0.1:41594",
"User-Agent":"Python-urllib/2.7"
},
"httpVersion":"1.1",
"method":"POST",
"post":"{\"sessionId\": \"074c7810-c3bf-11e5-beca-8f942aed378d\", \"id\": \":wdc:1453766104559\"}",
"url":"/click",
"urlParsed":{
"anchor":"",
"query":"",
"file":"click",
"directory":"/",
"path":"/click",
"relative":"/click",
"port":"",
"host":"",
"password":"",
"user":"",
"userInfo":"",
"authority":"",
"protocol":"",
"source":"/click",
"queryKey":{
},
"chunks":[
"click"
]
},
"urlOriginal":"/session/074c7810-c3bf-11e5-beca-8f942aed378d/element/%3Awdc%3A1453766104559/click"
}
}
假装不是带有自定义
User-Agent
头的PhantomJS
:为我工作。在
您还可以通过添加fluent explicit waits来改进测试。例如,等待元素可见:
^{pr2}$相关问题 更多 >
编程相关推荐