硒与pyvirtualdisplay无法定位elemen

2024-03-29 10:28:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个使用selenium登录站点的工作脚本,如下所示:

脚本.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

通过ssh在一个amazon ubuntu框中运行该脚本,我按以下方式安装了firefox: sudo apt-get install firefox

我得到的错误是:

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

如果我也通过ssh在另一个ubuntu框上运行相同的脚本,它运行得很好,没有错误,但是我不知道firefox是如何安装在那个框上的,可能是什么原因导致了这个错误。是否安装了相关的firefox,以及如何正确安装它以与pyvirtualdisplay和selenium一起使用?


Tags: fromimportbrowser脚本idgetselenium错误
2条回答
from pyvirtualdisplay import Display 

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import Select

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

from selenium.common.exceptions import NoSuchElementException

from selenium.common.exceptions import NoAlertPresentException

from selenium.webdriver.common.keys import Keys

import unittest, time, re, random

capabilities = DesiredCapabilities.FIREFOX.copy()

capabilities['marionette'] = False

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

driver = webdriver.Firefox(capabilities=capabilities)

driver.implicitly_wait(20)

base_url = "http://xxx.yyy.zzz.aaa/sss/sss-Login/login/main_login.php"

RANDINT = random.random()*10000

verificationErrors = []

driver.get(base_url + "")

username = driver.find_element_by_id("myusername")

username.send_keys("xxxxxxxx")

driver.implicitly_wait(20)

password = driver.find_element_by_id("mypassword")

 #password.send_keys("xxxxzz" + Keys.ENTER)

password.send_keys("xxxxzzc" )

driver.implicitly_wait(20)

driver.find_element_by_xpath("//*[@id='submit']").click() 


 # Click on category link 


driver.find_element_by_xpath("//*[@id='stylefour']/ul/li[3]/a").click()

driver.find_element_by_xpath("//*[@id='stylefour']/ul/li[1]/a").click()

driver.find_element_by_xpath("//*[@id='stylefour']/ul[2]/li[4]/a").click

 # Click on sub-category link

driver.find_element_by_xpath("//*[@id='top']/body/div/div[2]/div[2]/div/div[2]/ul/li[4]/a/span").click()

 # Click on product image

driver.find_element_by_xpath("//*[@id='product-collection-image-374']").click()

 # Click Checkout button

driver.find_element_by_xpath("//*[@id='checkout-button']/span/span").click()

driver.find_element_by_id("billing:firstname").clear()

driver.find_element_by_id("billing:firstname").send_keys("selenium", RANDINT, "_fname")

driver.find_element_by_id("billing:lastname").clear()

driver.find_element_by_id("billing:lastname").send_keys("selenium", RANDINT, "_lname")

 # Click Place Order

driver.find_element_by_xpath("//*[@id='order_submit_button']").click()



driver.quit()

display.stop()

如果网站上有一些动态内容,您需要等待一段时间 直到你能找回想要的元素。尝试以下代码示例:

检查配置

  • 是否为pyvirtualdisplay安装了后端,如xvfbxephyr? 如果不是

    尝试:sudo apt-get install xvfb xserver-xephyr

第一次尝试:添加一个简单的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

第二次尝试:browser.implicitly_wait(30)添加到Selenium Web驱动程序中。

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

相关问题 更多 >