1

Basically I want to automate google search engine which basically search and click on the first link that is populated after search button is clicked so i am using python do this. i am successfully able to search the things but not able to click on the link after page reload

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

url = "https://www.google.com"
driver = webdriver.Chrome("C:/Users/User/Python/AllCodes/chromedriver.exe")

driver.get(url)
insert = driver.find_element_by_name("q")
insert.send_keys("K.J.Somaiya")
button = driver.find_element_by_name("btnK")
button.submit()
# after this new page reload and link are poluated

linktext = driver.find_element_by_link_text("K. J. Somaiya Institute of Management")
linktext.submit()

This input class i used name which is q

<input class="gLFyf gsfi" jsaction="paste:puy29d;" maxlength="2048" name="q" type="text" aria-autocomplete="both" aria-haspopup="false" autocapitalize="off" autocomplete="off" autocorrect="off" autofocus="" role="combobox" spellcheck="false" title="Search" value="" aria-label="Search" data-ved="0ahUKEwj9qOfEmLzvAhWSj-YKHZUYDngQ39UDCAQ">

This the html code from i target linktext which is K.J.Somaiya Inst.....

  • Add some HTML code, and exlain wat is your issue with error code. Do you want to reload and be on google search page or to be transfered to link that you searched for? – Gaj Julije Mar 19 '21 at 11:43
  • 1
    Did you try `linktext.click()` in stead of `linktext.submit()`? – Jortega Mar 19 '21 at 11:47
  • @GajJulije i have posted html code in image file – Hari The Flemish Mar 19 '21 at 12:59
  • "C:\Users\User\Python\login.py", line 16, in driver.find_elements_by_class_name("LC20lb DKV0Md").click() AttributeError: 'list' object has no attribute 'click' – Hari The Flemish Mar 19 '21 at 13:09
  • @Hari The Flemish No that is not how you should do it in this particular case. The link is not a standarf href it is a text related to class. So you need to find element which have class (if calss name is static, not changing) like driver.find_element_by_class_name("class_of_element") and than get text wich is your link. Lik on https://stackoverflow.com/questions/28022764/python-and-how-to-get-text-from-selenium-element-webelement-object – Gaj Julije Mar 19 '21 at 13:15
  • Welcome to Stack Overflow! 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. This includes HTML. – JeffC Mar 19 '21 at 14:16
  • 1
    @GajJulije Thanks as this class is dynamically changing so i have to such class which is not changing I got your point – Hari The Flemish Mar 19 '21 at 16:39
  • @JeffC Thank you sir next time i will be careful and won't post the screenshot – Hari The Flemish Mar 19 '21 at 16:40

1 Answers1

1

you need to wait until that element becomes visible in dom. Read about selenium waits here

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
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.google.com"
driver = webdriver.Chrome("C:/Users/User/Python/AllCodes/chromedriver.exe")

driver.get(url)
insert = driver.find_element_by_name("q")
insert.send_keys("K.J.Somaiya")
button = driver.find_element_by_name("btnK")
button.submit()
# after this new page reload and link are poluated
linktext = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "K. J. Somaiya"))
    ) # Waits 10 second before element loads.

linktext.click()

George Imerlishvili
  • 1,816
  • 2
  • 12
  • 20
  • `# Waits 10 second before element loads.` Just to be clear... it doesn't actually wait 10s, it waits *up to* 10s. It checks immediately and if the check passes, it stops waiting and the code continues. If the check fails, it tries (polls) every 500ms up to 10s until it succeeds and continues or times out, throwing an exception. – JeffC Mar 19 '21 at 18:22