Skip to content
Advertisement

selenium xpath unable to identify class visually seen in HTML

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.

JavaScript

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:

JavaScript

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

Advertisement

Answer

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.

JavaScript

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

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement