-1

I'm scraping a website. I'm trying to click on a link under <li> but it throws NoSuchElementException exception.

And the links I want to click:

enter image description here

I'm using below code:

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

The error I got:

/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

in find_element_by_link_text(self, link_text) 426 element = driver.find_element_by_link_text('Sign In') 427 """ --> 428 return self.find_element(by=By.LINK_TEXT, value=link_text) 429 430 def find_elements_by_link_text(self, text):

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

in find_element(self, by, value) 976 return self.execute(Command.FIND_ELEMENT, { 977 'using': by, --> 978 'value': value})['value'] 979 980 def find_elements(self, by=By.ID, value=None):

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

in execute(self, driver_command, params) 319 response = self.command_executor.execute(driver_command, params) 320 if response: --> 321 self.error_handler.check_response(response) 322 response['value'] = self._unwrap_value( 323 response.get('value', None))

/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 raise exception_class(message, screen, stacktrace, alert_text) --> 242 raise exception_class(message, screen, stacktrace) 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)
NarendraR
  • 7,577
  • 10
  • 44
  • 82
  • 1
    Your error comes from code `webdriver.find_element_by_css_selector("[href*='prestamos/prestamo-Coche Nuevo']")`, but we can't see this code in your given code. Please confirm your given code is correct. – yong Mar 13 '19 at 10:03
  • I updated my question :) – Tiffany Zucman Mar 13 '19 at 10:31
  • Please read why [a screenshot of code is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Paste the code and properly format it instead. – JeffC Mar 13 '19 at 18:43
  • If you read the link you would see that screenshots of code are not allowed and why. Please remove the screenshot of HTML and replace it with the actual text of the HTML. – JeffC Mar 13 '19 at 18:52

2 Answers2

0

You could simply grab that url and get to it. Also, worth noting that you have a base url you can simply add the hyphenated search string to i.e. financiar-viaje on to base of https://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)
QHarr
  • 83,427
  • 12
  • 54
  • 101
  • It's working. I have another question: how do I get the same result from this link? Https://www.cofidis.es/es Thanks in advance. – Tiffany Zucman Mar 13 '19 at 10:52
  • Hi, just stepping in to meeting. Will update for you after. – QHarr Mar 13 '19 at 10:56
  • I have two other very important questions for me. If you have time to see, I would be very grateful. Here are the links: https://stackoverflow.com/questions/55099298/unable-to-locate-element-selenium-webdriver/55125742#55125742 https://stackoverflow.com/questions/55119878/click-a-partial-div-link-using-python-selenium/55120424?noredirect=1#comment96989521_55120424 – Tiffany Zucman Mar 13 '19 at 12:37
0

Use below code to click on the link

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

Use implicit/explicit wait to make sure your element is ready to interact. in your case :

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')
NarendraR
  • 7,577
  • 10
  • 44
  • 82
  • 1
    Thank you very much for your reply. I have already accepted an answer, and I do not have enough point for up vote your answer. – Tiffany Zucman Mar 13 '19 at 12:26
  • Np. glad to here :) – NarendraR Mar 13 '19 at 12:32
  • I have two other very important questions for me. If you have time to see, I would be very grateful. Here are the links: https://stackoverflow.com/questions/55099298/unable-to-locate-element-selenium-webdriver/55125742#55125742 https://stackoverflow.com/questions/55119878/click-a-partial-div-link-using-python-selenium/55120424?noredirect=1#comment96989521_55120424 – Tiffany Zucman Mar 13 '19 at 12:37