首页 文章

为什么硒只单击代码的第一行而不单击其他两行?

提问于
浏览
2

我首先测试了第一行代码是否可以单击 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?
    }

1 回答

  • 1

    在定位元素之前添加一些等待以使页面加载

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.name("button")));
    button.click();
    

    第三个定位器By.linkText("Cart")无效,因为按钮没有Cart文本,它位于data-textvalue属性中。

    附带说明,查找部分文本时应使用By.partialLinkText()

相关问题