[SOLVED] selenium xpath unable to identify class visually seen in HTML


This Content is from Stack Overflow. Question asked by Cyrus

I am trying to click on the rate button to set the rating for episode one of Atlanta season four on the IMDB website as illustrated by the image further below.

I have tried various combinations of selenium xpath values and have googled various links to attempt to achieve this, but can’t remember which now. My current python is below.


xpath = './/*[@class="list_item odd"]|.//*[@class="list_item even"]'
div = driver.find_element(By.XPATH, xpath)

xpath = './/*[@class="ipl-rating-star ipl-rating-interactive__star--empty"]'
div2 = div.find_element(By.XPATH, xpath)

selector = "spam>svg>path"
class2 = div2.find_element(By.CSS_SELECTOR, selector)

The div variable is assigned as I want with a single element; but the python fails to find the attribute value ‘ipl-rating-star ipl-rating-interactive__star–empty’ , though it can be seen in the html copied from the web page below:

<div class="ipl-rating-star ipl-rating-interactive__star--empty ">
    <span class="ipl-rating-star__star">
        <svg class="ipl-icon ipl-star-border-icon  " xmlns="http://www.w3.org/2000/svg" fill="#000000" height="24" viewBox="0 0 24 24" width="24">
            <path d="M22 9.24l-7.19-.62L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21 12 17.27 18.18 21l-1.63-7.03L22 9.24zM12 15.4l-3.76 2.27 1-4.28-3.32-2.88 4.38-.38L12 6.1l1.71 4.04 4.38.38-3.32 2.88 1 4.28L12 15.4z"></path>
            <path d="M0 0h24v24H0z" fill="none"></path>
    <span class="ipl-rating-star__rating">Rate</span>

The python currently produces this error message: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {“method”:”xpath”,”selector”:”.//*[@class=”ipl-rating-star ipl-rating-interactive__star–empty”]”}
(Session info: chrome=105.0.5195.127)

I am also using PyCharm 2022.2.2 Build #PC-222.4167.33 with Python 3.10.7

enter image description here


You are using wrong locators.
Also you need to add delays to wait for elements to become clickable.
The best practice here is to use WebDriverWait expected conditions explicit waits.
The following code is working.
It actually contains only 2 clicks.
I’m selecting the rating 6 here, but you can select any other value as well.
This can also be done with CSS Selectors instead of XPath.

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

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
url = "https://www.imdb.com/title/tt4288182/episodes/?ref_=tt_ov_epl"
wait = WebDriverWait(driver, 20)

wait.until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class,'ipl-rating-interactive__star--')]//span[@class='ipl-rating-star__rating']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[@href='#void'][@data-value='6']//div[contains(@class,'ipl-rating-interactive__star--')]//span"))).click()

After these to clicks you need to login in order to perform rating
enter image description here

This Question was asked in StackOverflow by Cyrus and Answered by Prophet It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?