【问题标题】:How to click on the list of the <a> elements in an <ul> and <li> elements with selenium using python?如何使用python单击带有硒的<ul>和<li>元素中的<a>元素列表?
【发布时间】:2019-08-03 22:16:24
【问题描述】:

我正在抓取website。我正在尝试单击&lt;li&gt; 下的链接,但它会引发NoSuchElementException 异常。

还有我想点击的链接:

我正在使用以下代码:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--start-maximized')
chrome_options.add_argument('window-size=5000x2500')
webdriver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
url = "https://www.cofidis.es/es/creditos-prestamos/financiacion-coche.html"
webdriver.get(url)
webdriver.find_element_by_xpath('//*[@id="btncerrar"]').click()
time.sleep(5)
webdriver.find_element_by_link_text('Préstamo Coche Nuevo').click()
webdriver.save_screenshot('test1.png')

我得到的错误:

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:10: DeprecationWarning: use options instead of chrome_options
  # Remove the CWD from sys.path while we load stuff.

---------------------------------------------------------------------------

NoSuchElementException                    Traceback (most recent call last)

<ipython-input-44-f6608be53ab3> in <module>()
     13 webdriver.find_element_by_xpath('//*[@id="btncerrar"]').click()
     14 time.sleep(5)
---> 15 webdriver.find_element_by_link_text('Préstamo Coche Nuevo').click()
     16 webdriver.save_screenshot('test1.png')

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py

在 find_element_by_link_text(self, link_text) 中 426 元素 = driver.find_element_by_link_text('登录') 第427章 --> 428 返回 self.find_element(by=By.LINK_TEXT, value=link_text) 429 430 def find_elements_by_link_text(自我,文本):

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py

in find_element(self, by, value) 第976章 第977章 --> 978 '值': 值})['值'] 979 980 def find_elements(self, by=By.ID, value=None):

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py

在执行中(self,driver_command,params) 319 响应 = self.command_executor.execute(driver_command,参数) 320 如果响应: --> 321 self.error_handler.check_response(响应) 322响应['值'] = self._unwrap_value( 323 response.get('value',无))

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py

in check_response(self, response) 240 alert_text = value['alert'].get('text') 241 引发异常类(消息、屏幕、堆栈跟踪、警报文本) --> 242 引发异常类(消息、屏幕、堆栈跟踪) 243 244 def _value_or_default(self, obj, key, default):

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Préstamo Coche Nuevo"}
  (Session info: headless chrome=72.0.3626.121)
  (Driver info: chromedriver=72.0.3626.121,platform=Linux 4.14.79+ x86_64)

【问题讨论】:

  • 您的错误来自代码webdriver.find_element_by_css_selector("[href*='prestamos/prestamo-Coche Nuevo']"),但我们在您给定的代码中看不到此代码。请确认您给定的代码是正确的。
  • 我更新了我的问题:)
  • 请阅读为什么a screenshot of code is a bad idea。粘贴代码并正确格式化。
  • 如果您阅读该链接,您会看到不允许截取代码以及原因。请删除 HTML 的屏幕截图,并将其替换为 HTML 的实际文本。

标签: python selenium web-scraping selenium-chromedriver


【解决方案1】:

您可以简单地抓取该网址并访问它。此外,值得注意的是,您有一个基本网址,您可以简单地将连字符的搜索字符串添加到 financiar-viajehttps://www.cofidis.es/es/creditos-prestamos/ 的基础上

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

url = 'https://www.cofidis.es/es/creditos-prestamos/financiacion-coche.html'
driver = webdriver.Chrome()
driver.get(url)
url =  WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "[href*='financiar-viaje']"))).get_attribute('href')
driver.get(url)

【讨论】:

【解决方案2】:

使用下面的代码点击链接

webdriver.find_element_by_css_selector("#ei_tpl_navertical li>a[data='16736']").click()

使用隐式/显式等待来确保您的元素已准备好进行交互。在你的情况下:

webdriver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
url = "https://www.cofidis.es/es/creditos-prestamos/financiacion-coche.html"
webdriver.get(url)
webdriver.implicitlyWait(20)
webdriver.find_element_by_id('btncerrar').click()
time.sleep(5)
webdriver.find_element_by_css_selector("#ei_tpl_navertical li>a[data='16736']").click()
webdriver.save_screenshot('test1.png')

【讨论】:

猜你喜欢
  • 2017-12-26
  • 2015-04-09
  • 2021-03-05
  • 2016-01-12
  • 1970-01-01
  • 2012-10-03
  • 1970-01-01
  • 2018-07-07
  • 2020-04-18
相关资源
最近更新 更多