selenium-webdriverweb-scrapingweb-crawlerhreftimeoutexception

Clicking on Href element with selenium


The code is trying to click on a href element in python Selenium but is failing. Here is the code.

#provides access to the webdriver
from selenium import webdriver
#allows interraction with elements of the webpage
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException

import time

#initilize a webdriver object
driver = webdriver.Chrome()
driver.get("https://www.dynamic-example.com/ab/search/fun")

   # click on game title
    game_title4 = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//h3[contains(@class,"game-title")]//a[contains(@href,"/games/")]/span')))
    # added a loop to scroll down and up until the element is located
    while not games_title4.is_displayed():
        driver.execute_script("window.scrollBy(0, 100);")
        game_title4 = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, '//h3[contains(@class,"game-title")]//a[contains(@href,"/game/")]/span')))
    # wait for the element to become clickable
        game_title4 = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '//h3[contains(@class,"games-title")]//a[contains(@href,"/games/")]/span')))
    # click on the element using execute_script
        driver.execute_script("arguments[0].click();", games_title4)

and here is the element it is trying to click on <div class="game-tile-badges d-flex mb-10 is-empty" data-test="gameTileBadges"><!----> <!----> <!----></div><h3 class="my-0 p-sm-right game-tile-title"><a href="/games/fun-clicking-activity_~0138709d12bc7bb5ad/">Fun Clicking Game“Fun activity”</a></h3>

i tried click with python and java, i also tried ccs and xpath , and webdriver wait. expecting the code to be able to find the element but it still struggles. the website changes the "game title" every day so i can only use a partial link "game" as that's in every game title but not the full link.


Solution

  • I cannot access this URL from my machine. Answering based on the provided HTML mark-up.

    If this is the HTML element:

    <h3 class="my-0 p-sm-right game-tile-title">
      <a href="/games/fun-clicking-activity_~0138709d12bc7bb5ad/">Fun Clicking Game“Fun activity”</a>
    </h3>
    

    The XPATH expressions which you have constructed will never work. If you look closely, the @class attribute's value contains game-tile-title and not game-title. Also, I do not see any span node. So the XPATH expression based on the provided HTML should be:

    //h3[contains(@class,'game-tile-title')]//a[contains(@href,'games')]