Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

python - Selenium with pyvirtualdisplay unable to locate element

I have a working script that logs into a site using selenium like this:

script.py

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(1024, 768))
display.start()

browser = webdriver.Firefox()
actions = webdriver.ActionChains(browser)
browser.get('some_url_I_need')
content = browser.find_element_by_id('content') # Error on this line

running that script on an amazon ubuntu box through ssh where I installed firefox the following way: sudo apt-get install firefox

The error I get is:

selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"id","selector":"content"}'

If I run the same script on another ubuntu box through ssh too, it runs fine, no error, but I don't know how firefox was installed on that box, what could be the cause of that error. Is is related firefox installation and how to properly install it to be used with pyvirtualdisplay and selenium ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If there is some dynamic content on the website you need to wait some time until you can retrieve the wished element. Try to following code examples:

Check Configuration

  • Did you install a backend for pyvirtualdisplay like xvfb and xephyr? If not,

    try: sudo apt-get install xvfb xserver-xephyr

First try: Add a simple time.sleep()

import time
from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(1024, 768))
display.start()

browser = webdriver.Firefox()
actions = webdriver.ActionChains(browser)
browser.get('some_url_I_need')
time.sleep(5) # sleep for 5 seconds
content = browser.find_element_by_id('content') # Error on this line

Second try: Add browser.implicitly_wait(30) to your Selenium webdriver.

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(1024, 768))
display.start()

browser = webdriver.Firefox()
browser.implicitly_wait(30) # seconds
actions = webdriver.ActionChains(browser)
browser.get('some_url_I_need')
content = browser.find_element_by_id('content') # Error on this line

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...