java - 为什么 selenium 只点击第一行代码而不点击另外两行?
问题描述
我首先测试了第一行代码是否可以单击 XPath,但是当我添加第二行代码以单击 By.name() 时它不起作用,所以我尝试在 XPath 中进行更改,然后在 CSS 选择器中进行更改但它只单击第一个(行的 XPath 代码)。我已经尝试过,但似乎没有单击其他两个元素。我发现它只点击第一页上的内容,并不关心新页面上的内容,我告诉点击我想做的元素。我正在使用Selenium version 3.141.59
.
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\ae65255\\Desktop\\java_gui\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://shop.palaceskateboards.com/collections/new");
driver.findElement(By.xpath("//*[@id=\"product-loop\"]/div[@data-alpha='S-LINE JOGGER BLACK']")).click(); //only this one work
driver.findElement(By.name("button")).click(); //second click dosen't work?
driver.findElement(By.linkText("Cart")).click(); //this dosen't work too?
}
解决方案
在定位元素之前添加一些等待让页面加载
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.name("button")));
button.click();
第三个定位器By.linkText("Cart")
不起作用,因为按钮没有Cart
文本,它在data-text
andvalue
属性中。
作为旁注,您应该By.partialLinkText()
在查找部分文本时使用。
推荐阅读
- amazon-web-services - AWS EMR Spark --properties-file 类 com.amazon.ws.emr.hadoop.fs.EmrFileSystem 未找到
- javascript - 缩放和平移在 D3 中不起作用 在鼠标滚轮上
- karate - 空手道:当我想将值设置为 $..somewhereInJsonPath 我得到路径不能以'结尾
- javascript - 从 javascript 调用 URL 处理程序
- c# - 当我的调试输出持续一秒时我该怎么办?
- java - ORA-29532: Java 调用被未捕获的 Java 异常终止: java.lang.NoClassDefFoundError
- javascript - 正则表达式使搜索建议像谷歌或亚马逊一样粗体
- excel - 根据另一个表excel中的值替换列的文本
- angular - 如果有 401 响应,角度拦截器会注销应用程序吗?
- angular - 如何在 Angular6 中执行自动滚动列表?