简体   繁体   English

硒python不适用于phantomjs但可与Firefox一起使用

[英]selenium python not working with phantomjs but works with Firefox

I am try to sort by most recent option using below code but it gives below error 我尝试使用下面的代码按最新选项进行排序,但出现以下错误

Error 错误

Message: {"errorMessage":"Unable to find element with css selector 'span.sortedLabel'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"107","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:34510","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"using\": \"css selector\", \"value\": \"span.sortedLabel\", \"sessionId\": \"f5246520-f7d4-11e7-a9f4-adb0a497f49e\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/f5246520-f7d4-11e7-a9f4-adb0a497f49e/element"}}

Screenshot: available via screen 屏幕截图:可通过屏幕获取

Everything works properly with Firefox, but not with phantomjs. 一切都适用于Firefox,但不适用于phantomjs。

Here is the code: 这是代码:

from selenium import webdriver  # open webdriver for specific browser
from selenium.webdriver.common.keys import Keys  # for necessary browser action
from selenium.webdriver.common.by import By  # For selecting html code
import time

url="https://www.magicbricks.com/property-for-rent/residential-real-estate?proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment,Service-Apartment,Residential-House,Villa&Locality=Bellandur&cityName=Bangalore"
# os.environ['NO_PROXY'] = '127.0.0.1'
# driver = webdriver.Firefox()
driver = webdriver.PhantomJS(executable_path=r'D:\Setup\NEW SOFTWARE\Python\phantomjs-2.1.1-windows\bin\phantomjs.exe')

driver.get(url)
try:
    #driver.find_element_by_id("sort4").click()
    driver.find_element_by_css_selector("span.sortedLabel").click()
    driver.find_element_by_id("sort4").click()
    time.sleep(3)
    html = driver.page_source
except Exception as e:
    print(str(e))

Any solution to this ? 有什么解决办法吗?

It looks like you haven't implemented any timeouts. 看来您尚未实现任何超时。 FF and PahntomJS might perform differently in terms of when a page is considered fully loaded and this can lead to a situation that FF works fine and PhantomJS does not. FF和PahntomJS在将页面视为已满载的情况下可能会执行不同的操作,这可能导致FF工作正常而PhantomJS不能正常工作。 To quickly debug this behavior, just add few implicit waits between your cations and see if they help. 要快速调试此行为,只需在阳离子之间添加一些隐式等待,看看它们是否有帮助。 If it works, replace the implicit waits with proper explicit waits. 如果可行,请将隐式等待替换为适当的显式等待。

An example of an implicit wait of 10 seconds: 隐式等待10秒的示例:

driver.implicitly_wait(10) # seconds

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM