【问题标题】:selenium python not working with phantomjs but works with Firefoxselenium python 不能与 phantomjs 一起使用,但可以与 Firefox 一起使用
【发布时间】:2018-01-12 20:23:27
【问题描述】:

我尝试使用下面的代码按最近的选项排序,但它给出了以下错误

错误

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

有什么解决办法吗?

【问题讨论】:

  • 我已经尝试调整窗口大小,但仍然没有成功。 driver.set_window_size(1024, 768) # 设置浏览器大小。

标签: python python-3.x python-2.7 selenium-webdriver phantomjs


【解决方案1】:

您似乎没有实施任何超时。 FF 和 PahntomJS 在页面被认为完全加载时可能会执行不同的操作,这可能导致 FF 工作正常而 PhantomJS 不能正常工作的情况。要快速调试此行为,只需在您的阳离子之间添加一些隐式等待,看看它们是否有帮助。如果可行,请将隐式等待替换为适当的显式等待。

隐式等待 10 秒的示例:

driver.implicitly_wait(10) # seconds

【讨论】:

  • 感谢您的回复。我在点击之前和之后添加了隐式等待,但仍然无法正常工作..
猜你喜欢
  • 1970-01-01
  • 2016-09-03
  • 2014-03-20
  • 2013-07-19
  • 2019-10-26
  • 1970-01-01
  • 1970-01-01
  • 2013-03-22
  • 1970-01-01
相关资源
最近更新 更多