[英]Unable to find the xpath of star using selenium & java
Thank you in advance !先感谢您 !
url - https://www.tripadvisor.in/UserReviewEdit-g641714-d1156207-Club_Mahindra_Madikeri_Coorg-Madikeri_Kodagu_Coorg_Karnataka.html网址 - https://www.tripadvisor.in/UserReviewEdit-g641714-d1156207-Club_Mahindra_Madikeri_Coorg-Madikeri_Kodagu_Coorg_Karnataka.html
i need to hover on the stars and select the 5th star.我需要将鼠标悬停在星星上并选择第 5 颗星。
please find my code :-请找到我的代码:-
private static void setRating(String star) {
//new Actions(driver).moveToElement(driver.findElement(By.xpath("//*[@id='qid10']/option[1]"))).perform();
List<WebElement> rating = driver.findElements(By.xpath("//span[@class='ui_bubble_rating fl bubble_00']"));
rating.size();
List<WebElement> ele = driver.findElements(By.xpath("//div[@class='easyClear bigRatingParent']"));
System.out.println(ele.size());
for(WebElement ratings:ele) {
System.out.println(ratings);
}
new Actions(driver).moveToElement(driver.findElement(By.xpath("//div[@class='question rating bigRating labelAndInput required ']/child::label/following-sibling::div/child::span"))).perform();
driver.findElement(By.xpath("//*[@id='qid10']/option[6]")).click();
}
}
Try using Java script executor尝试使用 Java 脚本执行器
JavascriptExecutor executor = (JavascriptExecutor)driver; JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("document.getElementById('bubble_rating').ui_bubble_rating fl bubble_05 bubble_50"); executor.executeScript("document.getElementById('bubble_rating').ui_bubble_rating fl bubble_05 bubble_50");
this would work, please kindly let me know这行得通,请让我知道
To select the 5 th star within https://www.tripadvisor.in/ you have to induce WebDriverWait for the visibilityOfElementLocated()
and you can use either of the following Locator Strategies :要选择内的5个星https://www.tripadvisor.in/你有诱导WebDriverWait的visibilityOfElementLocated()
你可以使用以下的定位策略:
xpath
: xpath
:
driver.get("https://www.tripadvisor.in/UserReviewEdit-g641714-d1156207-Club_Mahindra_Madikeri_Coorg-Madikeri_Kodagu_Coorg_Karnataka.html]"); new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@id='bubble_rating']"))), 50, 0).click().build().perform();
cssSelector
: cssSelector
:
driver.get("https://www.tripadvisor.in/UserReviewEdit-g641714-d1156207-Club_Mahindra_Madikeri_Coorg-Madikeri_Kodagu_Coorg_Karnataka.html]"); new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("span#bubble_rating"))), 50, 0).click().build().perform();
Browser Snapshot:浏览器快照:
Please try this piece of code.请尝试这段代码。 Do let me know in case you need help.如果您需要帮助,请告诉我。
import java.time.Duration;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class TripAdvisor
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","C:\\selenium\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseUrl = "https://www.tripadvisor.in/"
+ "UserReviewEdit-g641714-d1156207-Club_Mahindra_Madikeri_Coorg-Madikeri_Kodagu_Coorg_Karnataka.html";
driver.get(baseUrl);
// Capture the webElement and its width
WebElement target = driver.findElement(By.xpath("//img[@alt='Roll over, then click to rate']/.."));
Dimension dimension = target.getSize();
int width= dimension.getWidth();
// Initialize the list of 5 star rating with no elements
List<WebElement> rating5star= driver.findElements(By.xpath("//span[@class='ui_bubble_rating fl bubble_50']"));
// When using the W3C Action commands, offsets are from the center of element
// This is in contradiction to the information provided in Selenium java docs
// https://selenium.dev/selenium/docs/api/java/org/openqa/selenium/interactions/Actions.html#moveToElement-org.openqa.selenium.WebElement-int-int-
// Hence the movement should be from -(width/2) to (width/2)
Actions action = new Actions(driver);
int x = (0 - width/2);
while(rating5star.size()==0 & x<=(width/2))
{
action.moveToElement(target, x, 0).click().pause(Duration.ofSeconds(1)).perform();
rating5star= driver.findElements(By.xpath("//span[@class='ui_bubble_rating fl bubble_50']"));
System.out.println(rating5star.size());
x=x+10;
}
}
} }
I think hover will not work as it is single element for whole rating bar.我认为悬停不起作用,因为它是整个评级栏的单个元素。 so what i have observed it will changed class
value for selected rating.所以我观察到它会改变所选评级的class
值。 We can do change rating with change class
attribute's value
with JS
我们可以使用JS
更改class
属性的value
来更改评级
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('bubble_rating').setAttribute('class', 'ui_bubble_rating fl bubble_50')");
OR或者
WebElement element = driver.findElement(By.id("bubble_rating"));
js.executeScript("arguments[0].setAttribute('class','ui_bubble_rating fl bubble_50')", element);
it will do the job.它会完成这项工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.