Reputation: 356
I am trying to click on the rate button to set the rating for the movie Mona Lisa and the Blood Moon 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 less logging onto the IMDB website to be able to effect this new rating.
driver.get('https://www.imdb.com/title/tt8760670/?ref_=nv_sr_srsg_0')
wait = WebDriverWait(driver, 20)
xpath = "(//div[@data-testid='hero-rating-bar__user-rating'])[1]//button"
wait.until(EC.element_to_be_clickable((By.XPATH, xpath))).click()
xpath = "//button[@aria-label='Rate 1']//svg"
wait.until(EC.element_to_be_clickable((By.XPATH, xpath))).click()
The first click works as I want; but the second fails. The HTML I'm trying to scrape is below:
<div class="ipc-starbar">
<div class="ipc-starbar__touch"></div>
<div class="ipc-starbar__rating">
<button class="ipc-starbar__rating__button" role="button" aria-label="Rate 1" tabindex="0">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" class="ipc-icon ipc-icon--star ipc-starbar__star ipc-starbar__star--active" id="iconContext-star" viewBox="0 0 24 24" fill="currentColor" role="presentation">
<path d="M12 17.27l4.15 2.51c.76.46 1.69-.22 1.49-1.08l-1.1-4.72 3.67-3.18c.67-.58.31-1.68-.57-1.75l-4.83-.41-1.89-4.46c-.34-.81-1.5-.81-1.84 0L9.19 8.63l-4.83.41c-.88.07-1.24 1.17-.57 1.75l3.67 3.18-1.1 4.72c-.2.86.73 1.54 1.49 1.08l4.15-2.5z"></path>
</svg>
</button>
The python currently produces this error message:
File "C:\Users\user\PycharmProjects\SeleniumProject1\Main\Show_Change.py", line 78, in
wait.until(EC.element_to_be_clickable((By.XPATH, xpath))).click()
File "C:\Users\user\PycharmProjects\SeleniumProject1\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 90, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
Backtrace:
I am using PyCharm 2022.2.2 Build #PC-222.4167.33 with Python 3.10.7 and chromedriver win32 105.0.5195.52
I asked a similar question before; but that was pertaining to an episode of a series, which has a completely different setup of HTML
I tried clicking the button above to set the rating to one; but my python crashed complaining that the button was unclickable.
Upvotes: 0
Views: 106
Reputation: 2678
Try to click the stars using Javascript executor, it is working:
stars = driver.find_element(By.XPATH,"(//*[@class='ipc-starbar__rating']/button)[1]")
driver.execute_script("arguments[0].click();", stars)
If you want to give more ratings change in number '1' in the xpath to whatever you want up to '10'
Upvotes: 1