1

I've gotten selenium to work perfectly but am wondering how I would go about clicking on the link of the string I've entered:

from selenium import webdriver  
from selenium.common.exceptions import NoSuchElementException  
from selenium.webdriver.common.keys import Keys  

url = "http://www.howlongtobeat.com"
driver = webdriver.Chrome()
driver.get(url)

search_element = driver.find_element_by_name("global_search_box")
search_element.clear()
search_element.send_keys("God of War (2018)")
search_element.send_keys(Keys.RETURN)

link = driver.find_element_by_xpath("//div id=[@class='nav_playthroughs_load]")
link.click()

how would I get it to click on the link entitled "God of War (2018)"

I'm getting this error:

WebDriverException                        Traceback (most recent call last)
<ipython-input-7-0343b11486bd> in <module>()
----> 1 search_element = driver.find_element_by_name("global_search_box")
      2 search_element.clear()
      3 search_element.send_keys("God of War (2018)")
      4 search_element.send_keys(Keys.RETURN)
      5 

~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in find_element_by_name(self, name)
    485             element = driver.find_element_by_name('foo')
    486         """
--> 487         return self.find_element(by=By.NAME, value=name)
    488 
    489     def find_elements_by_name(self, name):

~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in find_element(self, by, value)
    953         return self.execute(Command.FIND_ELEMENT, {
    954             'using': by,
--> 955             'value': value})['value']
    956 
    957     def find_elements(self, by=By.ID, value=None):

~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    310         response = self.command_executor.execute(driver_command, params)
    311         if response:
--> 312             self.error_handler.check_response(response)
    313             response['value'] = self._unwrap_value(
    314                 response.get('value', None))

~/anaconda3/lib/python3.6/site-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):

WebDriverException: Message: chrome not reachable
  (Session info: chrome=67.0.3396.79)
  (Driver info: chromedriver=2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881),platform=Linux 4.15.0-23-generic x86_64)

thanks,

littlejiver
  • 255
  • 2
  • 13
  • Possible duplicate of [Selenium click on link](https://stackoverflow.com/questions/11311100/selenium-click-on-link) – d219 Jun 16 '18 at 22:30
  • that post doesn't really help I think I'm having a problem locating the correct html – littlejiver Jun 16 '18 at 22:39
  • What is the difference between your this post and [this](https://stackoverflow.com/questions/50892591/cant-find-the-element-i-need-in-javascript-with-selenium-to-click-on-link/50894361#50894361) one? – demouser123 Jun 17 '18 at 07:18
  • @demouser123 it's basicly the same – littlejiver Jun 17 '18 at 08:48
  • 1
    Possible duplicate of [can't find the element I need in Javascript with selenium to click on link](https://stackoverflow.com/questions/50892591/cant-find-the-element-i-need-in-javascript-with-selenium-to-click-on-link) – demouser123 Jun 18 '18 at 06:17

1 Answers1

0

To click on the link with text as God of War (2018) you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:

  • LINK-TEXT:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "God of War (2018)"))).click()
    
  • CSS-SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.text_blue[title^='God of War']"))).click()
    
  • XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='text_blue'][@title=\"God of War (2018)\"]"))).click()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352