Search code examples
pythonseleniumautomationnew-operator

How can we click on the links after new page reload using python?


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.....


Solution

  • 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()