Selenium WebDriver:Firefox无法通过标记名称获取元素

时间:2018-06-27 13:04:26

标签: python selenium webdriver selenium-firefoxdriver headless-browser

我想在Python中使用Selenium WebDriver无头运行Firefox。

关键是要转到一个页面,等待JavaScript加载,然后收集此页面上的所有链接。

要开始测试,我做了以下代码:

import time 
from selenium import webdriver
from selenium.webdriver.firefox.options import Options


options = Options()
options.add_argument("--headless")

url = "http://localhost:3000/"

driver = webdriver.Firefox(firefox_options=options)
driver.get(url) 
time.sleep(5)

urls = driver.find_elements_by_tag_name('a') 
print(urls)

driver.quit()

这总是会出现以下错误:

Traceback (most recent call last):
  File "sel.py", line 18, in <module>
    urls = driver.find_elements_by_tag_name('a') 
  File "/home/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 545, in find_elements_by_tag_name
    return self.find_elements(by=By.TAG_NAME, value=name)
  File "/home/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 995, in find_elements
    'value': value})['value'] or []
  File "/home/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 318, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/home/petra/.local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 472, in execute
    return self._request(command_info[0], url, body=data)
  File "/home/petra/.local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 496, in _request
    resp = self._conn.getresponse()
  File "/usr/lib/python2.7/httplib.py", line 1136, in getresponse
    response.begin()
  File "/usr/lib/python2.7/httplib.py", line 453, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python2.7/httplib.py", line 417, in _read_status
    raise BadStatusLine(line)
httplib.BadStatusLine: ''

我尝试删除此行time.sleep(5),因为我认为这可能是问题所在。

现在print(urls)返回以下内容:

[<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="27257d43-81ec-48e4-9ed2-55709a23d60f", element="e728d5ef-001f-4335-bd57-19a1f2d82683")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="27257d43-81ec-48e4-9ed2-55709a23d60f", element="2c59c828-8557-48cc-a79a-02ea3c9d2d65")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="27257d43-81ec-48e4-9ed2-55709a23d60f", element="e2058a00-9bad-4f0c-8e2d-a236a567dddd")>]

如果我将time.sleep(0)放到time.sleep(4)上,则会显示此输出。

无论哪种方式,这都不是我想要的输出;我想查看页面上的所有锚点。

我在做什么错了?

对不起,我对此并不陌生。

2 个答案:

答案 0 :(得分:1)

尝试以下代码:

from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


urls = ui.WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.TAG_NAME, "a")))

for url in urls:

    print(url.get_attribute("href"))

# Another example of printing URLs (where actual_urls is a list of anchors).
actual_urls = [url.get_attribute("href") for url in urls]
print(actual_urls)

希望有帮助!

答案 1 :(得分:0)

可能存在动态加载元素的问题,希望对您有所帮助。请仔细阅读此问题,它提供了动态元素的解决方案。

How to wait until an element is present in Selenium?