簡體   English   中英

硒python不適用於phantomjs但可與Firefox一起使用

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

我嘗試使用下面的代碼按最新選項進行排序,但出現以下錯誤

錯誤

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"}}

屏幕截圖:可通過屏幕獲取

一切都適用於Firefox,但不適用於phantomjs。

這是代碼:

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))

有什么解決辦法嗎?

看來您尚未實現任何超時。 FF和PahntomJS在將頁面視為已滿載的情況下可能會執行不同的操作,這可能導致FF工作正常而PhantomJS不能正常工作。 要快速調試此行為,只需在陽離子之間添加一些隱式等待,看看它們是否有幫助。 如果可行,請將隱式等待替換為適當的顯式等待。

隱式等待10秒的示例:

driver.implicitly_wait(10) # seconds

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM