0

I'm very new to Selenium and I'm trying to make the code click on the right google web search link. If I add or remove the s for:(driver.find_element_by_link_text)it still doesn't work and when it does, click() doesn't. I could only get one of the two working not both.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException 
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome(executable_path='Python\chromedriver.exe')
driver.get('https://www.google.com/')

element = driver.find_element_by_name('q')
element.send_keys('Villanova')
element.send_keys(Keys.ENTER)

element = driver.find_element_by_link_text('Villanova College - King City').click()


time.sleep(5)
driver.close()
Reedinationer
  • 5,388
  • 1
  • 11
  • 33
  • You never use your import `NoSuchElementException` can it be removed from your post? – Reedinationer Apr 04 '19 at 04:20
  • Welcome to SO. `find_element_by_link_text` will return an element where as `find_elements_by_link_text` will return a list of elements so you can not click when using `find_elements_by_link_text`. However, check in the chrome dev tools if you can find that text before trying to click. Also consider the synchronization, means let the results load and then find the element. – supputuri Apr 04 '19 at 04:24
  • Is this the error you are getting: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Villanova College - King City"} – Sarthak Gupta Apr 04 '19 at 05:05
  • Within the Google Search Result page there is no _linkText_ as **Villanova College - King City** – undetected Selenium Apr 04 '19 at 05:37

1 Answers1

0

The link text "Villanova College - King City" is not present in the DOM. The text which is present is "villanova college king city" without hyphen. Also the element you are looking for not visible.

Earlier error :

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Villanova College - King City"}

Following is the error logs are generated with "villanova college king city":

>>> driver.find_element_by_xpath("//div[text()='villanova college king city']").click()
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    driver.find_element_by_xpath("//div[text()='villanova college king city']").click()
  File "C:\Users\sagupta\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\sagupta\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\sagupta\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\sagupta\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
  (Session info: chrome=72.0.3626.121)
  (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.17134 x86_64)
Sarthak Gupta
  • 382
  • 1
  • 10