Home > Software engineering >  Webdriver wont click on a second link
Webdriver wont click on a second link

Time:05-30

I want to make a bot using selenium, but I'm having trouble with my bot going to a different part of a website. In my code, my driver successfully goes to nike.com (1), then successfully clicks and loads a different link within Nike (clicks circled area in (1) and goes to (2)). Then, my problems begin here, I try to click and load a different link (2) but my driver does nothing. I know my driver found the second link because if I print out 'second.text' then I get the correct text (3)...

I am still new to selenium and I pretty much don't know what I am doing. Any help would be helpful. Thank you.

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    from webdriver_manager.chrome import ChromeDriverManager
    
    if __name__ == '__main__':
        options = Options()
        options.add_argument("start-maximized")
        driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

        driver.get("https://www.nike.com/men")

        driver.implicitly_wait(5)
        first = driver.find_element(by=By.CLASS_NAME, value="prl3-sm")
        first.click()

        driver.implicitly_wait(5)
        second = driver.find_element(by=By.CSS_SELECTOR, value='a[]')
        #print(second.text)
        second.click()

CodePudding user response:

I have tested it. Through Javascript click its getting clicked.

here is the code to click on second link.

second = driver.find_element(by=By.CSS_SELECTOR, value='a[]')
driver.execute_script("arguments[0].click();",second)

BTW you may need to define xpath properly. Example the second link pointing to 6 elements. But anyway through Javascript sclick it would click the 1st option

  • Related