繁体   English   中英

单击带有 Selenium 和 Java 的按钮

[英]Clicking button with Selenium and Java

我目前正在做一个项目,我可以 select 所有元素都没有一个,它真的令人沮丧,代码如下

<a class="btn btn-default button button-medium" href="http://automationpractice.com/index.php?controller=order" title="Proceed to checkout" rel="nofollow">
                        <span>
                            Proceed to checkout<i class="icon-chevron-right right"></i>
                        </span>
                    </a>

我尝试过 xpath、css、linktext、title 和其他方法,但没有运气,任何帮助都会真正得到帮助

我用过的 xpath 的例子

driver.findElement(By.xpath("/html//div[@id='layer_cart']//a[@title='Proceed to checkout']/span")).click();

我用过的 css 的例子

driver.findElement(By.cssSelector("a[title='Proceed to checkout'] > span")).click();

要在元素上click() ,您可以使用以下任一定位器策略

  • cssSelector

     driver.findElement(By.cssSelector("a.btn.btn-default.button.button-medium[title='Proceed to checkout'] > span")).click();
  • xpath

     driver.findElement(By.xpath("//a[@class='btn btn-default button button-medium' and @title='Proceed to checkout']/span")).click();

理想情况下,要在需要为elementToBeClickable()诱导WebDriverWait的元素上click() ,您可以使用以下任一定位器策略

  • cssSelector

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.btn.btn-default.button.button-medium[title='Proceed to checkout'] > span"))).click();
  • xpath

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='btn btn-default button button-medium' and @title='Proceed to checkout']/span"))).click();

您可以使用按钮 CSS 上的图标:

.icon-chevron-right right

或 xpath:

//i[@class='icon-chevron-right right']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM