我正在尝试将文本发送到google flights出发城市输入框。我可以找到它,但是当我尝试用send_keys
发送文本时,我得到了错误element not visible
。selenium怎么可能找到输入框,但当我向它发送键时,它就不可用了。我没有这个错误,直到我从firefox切换到chrome作为我的网络驱动程序。我的代码在下面
import
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class Bot:
def __init__(self):
# self.browser = webdriver.Firefox(executable_path='./geckodriver')
self.browser = webdriver.Chrome('./chromedriver')
self.departure_city = "COU"
self.destination_city = "HND"
self.departure_day = "December 1"
self.return_day = "December 10"
self.prices = []
self.Run()
def Run(self):
try:
self.SetFlight()
self.SetDates()
self.FindPrices()
self.SendText()
time.sleep(10)
self.browser.quit()
except Exception as ex:
print(ex)
self.browser.quit()
def SetFlight(self):
self.browser.get('https://www.google.com/flights/');
departure_take_off_boxes = self.browser.execute_script(
"return document.querySelectorAll('.EIGTDNC-Kb-f.EIGTDNC-Kb-b')")
print(departure_take_off_boxes[0].get_attribute('outerHTML'))
print(departure_take_off_boxes[1].get_attribute('outerHTML'))
self.browser.implicitly_wait(20)
departure_take_off_boxes[0].send_keys(self.departure_city)
departure_take_off_boxes[0].send_keys(Keys.RETURN)
time.sleep(1)
# departure_take_off_boxes[1].send_keys(self.destination_city)
# departure_take_off_boxes
尝试以下操作:
有关详细信息,请参阅以下文档的第7.2节:http://selenium-python.readthedocs.io/api.html
分析:
Selenium可以从页面中找到元素,只表示元素在页面源代码中有html代码,不等于元素在页面上可见。比如isPresent()和isDisplay()之间的区别。在
对于硒如何确定元素是可见的,我知道以下规则:
1元素大小不为零
2元素“display”css值不是“none”
我觉得很奇怪为什么你可以用firefox驱动程序运行pass,但是在chrome驱动程序上,我想甚至W3C上的webdirver规范都定义了这一点,也许firefox和chrome在webdriver中实现了这一点。在
回到输入框不可见的原因,我查看了输入框周围的html代码,我注意到在输入框的顶部有一个div层覆盖,并且输入框的大小在css样式中设置为1x1。
我尝试隐藏覆盖的div层,取消选中css中的width和height设置,之后您可以看到它,我可以输入值。在
我不确定,Selenium使用以下规则来确定元素是否可见:
但从用户体验来看,用户看不到这个元素,selenium API尽量接近用户体验。在
解决方案:
即使你把元素放到了div层,但是它的大小太小了,即使你输入了一些值,你也看不到你输入的文本。
这是一个UI设计问题,您需要与开发团队沟通。在
相关问题 更多 >
编程相关推荐