Search code examples
pythonseleniumxpathselenium-chromedriverwebdriverwait

using selenium with python to google href " Getting this error NoSuchElementException: Message: no such element: Unable to locate element: "


using selenium with python to google href or click on the links to open them but Getting this error NoSuchElementException: Message: no such element: Unable to locate element: "

This is the Code I wrote to do it.

url = "https://www.google.com/search?q="+text_fetch # The Dutch Dress in Orange—Why?
    driver.get(url)
    time.sleep(7)
    Ggl_results = driver.find_element(By.XPATH, '//*[@id="rso"]/div[1]/div/div/div[1]/div/a')  # finds webresults
    time.sleep(7)
    for result in Ggl_results:
        print(result.get_attribute("href"))

Solution

  • Instead of '//*[@id="rso"]/div[1]/div/div/div[1]/div/a' this XPath should work: //*[@id="rso"]//a[@data-ved][@ping]. I tested that on other random inputs and it worked.
    Also, instead of hardcoded sleeps time.sleep(7) WebDriverWait expected_conditions explicit waits should be used.
    This coded worked:

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = Options()
    options.add_argument("start-maximized")
    
    webdriver_service = Service('C:\webdrivers\chromedriver.exe')
    driver = webdriver.Chrome(options=options, service=webdriver_service)
    wait = WebDriverWait(driver, 10)
    
    url = "https://www.google.com/search?q=The Dutch Dress in Orange—Why?"
    
    driver.get(url)
    results = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//*[@id='rso']//a[@data-ved][@ping]")))
    for result in results:
            print(result.get_attribute("href"))
    

    This is the output:

    https://www.dutchamsterdam.nl/321-why-the-dutch-wear-orange
    https://netherlandsinsiders.com/why-is-the-national-color-of-the-netherlands-orange/
    https://www.distractify.com/p/why-do-netherlands-wear-orange
    https://dutchreview.com/culture/history/why-do-the-netherlands-love-orange-the-full-explainer/
    https://www.quora.com/Why-do-the-Dutch-wear-orange-when-their-flag-doesnt-have-orange-in-it
    https://aboutthenetherlands.com/the-reason-why-dutch-people-like-the-orange-color/
    https://www.iamexpat.nl/lifestyle/lifestyle-news/most-googled-why-do-dutch-wear-orange
    https://taiwanholland.com/holland-a-z/how-to-dress-up-orange-like-the-dutch/
    https://amsterdamhangout.com/why-is-orange-the-national-colour-of-the-netherlands/