Search code examples
javaselenium-webdriverratingbartestng-eclipse

Unable to select the rating for a content using Selenium Webdriver


I have the below code to select a rating for a published content. But when i use the click() method, it selects the num1start even if i give the xpath to select the num3 star. I tried using Actions and still no luck. Using Mac machine, Selenium Webdriver, Java, TestNG

enter image description here

<ul class="star-rating">
   <li onmouseout="showPreset()" onmouseover="hidePreset()">
       <a class="num1-stars" title="Rate this 1 stars out of 5" onclick="return submitNumRating("1");" href="#"></a>
   </li>
   <li onmouseout="showPreset()" onmouseover="hidePreset()">
       <a class="num2-stars" title="Rate this 2 stars out of 5" onclick="return submitNumRating("2");" href="#"></a>
   </li>
   <li onmouseout="showPreset()" onmouseover="hidePreset()">
       <a class="num3-stars" title="Rate this 3 stars out of 5" onclick="return submitNumRating("3");" href="#"></a>
   </li>
   <li onmouseout="showPreset()" onmouseover="hidePreset()">
       <a class="num4-stars" title="Rate this 4 stars out of 5" onclick="return submitNumRating("4");" href="#"></a>

Below are the code i tried 1-click() method - selects the 1st star

driver().findElement(By.xpath("//ul[@class=\"star-rating\"]/li/a[@class=\"num3-stars\"]")).click();

2- Using Actions - Sill selects 1st star

Actions actions = new Actions(driver);
WebElement star = getDriver().findElement(By.xpath("//ul[@class=\"star-rating\"]/li/a[@class=\"num3-stars\"]"));
actions.moveToElement(star);
actions.click().perform();

What should i do to select the star element correctly?


Solution

  • Try the below code and let me know.

    driver.findElement(By.xpath("//ul[@class='star-rating']/li[3]/a")).click();