selenium - 使用 selenium ,我试图在兄弟姐妹中使用 x 路径从一个元素移动到另一个元素
问题描述
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Demo {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\5558\\Desktop\\Selenium\\chromedriver.exe" );
WebDriver driver=new ChromeDriver();
driver.get("https://www.amazon.in"); //url in the browser
System.out.println(driver.getTitle());
driver.findElement(By.xpath("//div[@id='nav-xshop']/a[1]/following-sibling::a[2]")).click();
}
}
需要打手机,但它正在流行,请帮助纠正
解决方案
代替
driver.findElement(By.xpath("//div[@id='nav-xshop']/a[1]/following-sibling::a[2]")).click();
使用此定位器:
driver.findElement(By.xpath("//a[contains(@href,'mobile-phones')]")).click();
您还应该在此处添加显式等待,因此您的代码应如下所示:
public class Demo {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\5558\\Desktop\\Selenium\\chromedriver.exe" );
WebDriver driver=new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.get("https://www.amazon.in"); //url in the browser
System.out.println(driver.getTitle());
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(@href,'mobile-phones')]")));
driver.findElement(By.xpath("//a[contains(@href,'mobile-phones')]")).click();
}
}
推荐阅读
- python - 如何使用列名格式化熊猫中的列
- python - pandas diff() 忽略 groupby
- php - Laravel 多语言无前缀路由
- r - 有没有办法编写一个函数,将函数的总和移动某个参数?
- c# - 为什么此 LINQ 语句返回 null 而不是 count=0 的 IEnumerable
- c# - 如果在范围内,则递归添加值
- javascript - 如何在现有的 Angular 项目中使用 React 组件?
- python - Python - 通过传递带有模型名称的字符串作为输入来获取 Scikit-Learn 分类器
- python - 使用 TensorFlow 对象检测 API eval.py 获取 mAP
- reactjs - 如何修复“在 DragSource(Component) 的上下文中找不到拖放管理器”