Python 使用PhantomJS和Firefox实现浏览器自动化,支持不同的浏览器

Python 使用PhantomJS和Firefox实现浏览器自动化,支持不同的浏览器,python,selenium,selenium-webdriver,phantomjs,browser-automation,Python,Selenium,Selenium Webdriver,Phantomjs,Browser Automation,为什么代码可以与webdriver.Firefox一起使用,而不能与webdriver.PhantomJS一起使用 from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.PhantomJS() # why not? # driver.set_window_size(1400, 1050) # driver = webdriver.Firefox() #

为什么代码可以与webdriver.Firefox一起使用,而不能与webdriver.PhantomJS一起使用

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.PhantomJS() # why not?
# driver.set_window_size(1400, 1050)

# driver = webdriver.Firefox() # Firefox 45, works correctly

driver.get("https://www.rec-registry.gov.au/rec-registry/app/public/lgc-register")
driver.find_elements_by_tag_name('button')[4].click() # status
# show the needed elements for the next action,
# enter(open the door) to the div.ms-drop area
driver.find_elements_by_class_name('ms-drop')[4].find_element_by_css_selector('ul>li:nth-child(12)').click()  # registered
driver.find_element_by_id('search-submit').send_keys(Keys.RETURN)  # search

driver.save_screenshot('lgc1.png')

您应该尝试使用
.click()
进行单击,而不是使用
发送密钥(keys.RETURN)
如下所示:

driver.find_element_by_id('search-submit').click() 

什么意思不适用于phantomjs??有什么例外吗??为什么你不使用
驱动程序。按id(“搜索-提交”)查找元素。单击()
而不是
驱动程序。按id(“搜索-提交”)查找元素。发送键(key.RETURN)
。??@SaurabhGaur在Phantomjs中会在div.ms-drop上停止,没有任何例外,但在Firefox中,我会得到包含所需数据的html表。之后,phantomjs应该点击复选框和搜索按钮。为什么要使用
send_keys(keys.RET)‌​URN)
???是的,这两种情况下都适用于Firefox。好的,但我建议您尝试在Firefox和phantomjs中使用
。单击()。在导入时间之后。sleep(3)我得到了所需的数据,这是最好的方法,我建议您尝试使用
WebDriverWait
在单击后等待表出现,而不是
time。sleep
…)我尝试使用WebDriverWait并在之前隐式地等待,但在这里和以前都不起作用:<代码>从selenium.webdriver.support.ui导入WebDriverWait
time.sleep(3)#>2,工作
驱动程序。隐式地等待(30)#>30,不工作
WebDriverWait(驱动程序,30)#>30,不工作WebDriverWait在具有特定条件的元素上使用??仅创建对象是不够的。您需要为元素提供某些条件…:)最后WebDriverWait通过以下方式工作:
WebDriverWait(驱动程序,4)。直到(lambda s:s.execute_script(“return jQuery.active==0”)