【问题标题】:How to click on a button in Selenium Java如何单击 Selenium Java 中的按钮
【发布时间】:2022-02-14 17:34:46
【问题描述】:

我正在尝试使用 Internet Explorer(请注意,该网站仅适用于 IE)和 Eclipse 单击海康威视相机登录页面内的按钮 这是按钮的代码

<button class="btn btn-primary login-btn" type="button" ng-click="login()"><label class="ng-binding" ng-bind="oLan.login">Login</label></button>
<label class="ng-binding" ng-bind="oLan.login">Login</label>

图片:

这是我的代码,一切正常,但按钮无法点击。

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;


public class test {

    public static void main(String[] args) {
        System.setProperty("webdriver.ie.driver","C:\\Users\\SAAD\\Downloads\\IEDriverServer_Win32_4.0.0\\IEDriverServer.exe");
        WebDriver driver = new InternetExplorerDriver();
        driver.get("http://10.67.0.230");
        driver.manage().window().maximize();
        WebElement Username = driver.findElement(By.id("username"));
        Username.click();
        Username.sendKeys("admin");
        WebElement Password = driver.findElement(By.id("password"));
        Password.click();
        Password.sendKeys("ezEqL7?Ss=g");
        WebElement Login = driver.findElement(By.className("btn.btn-primary.login-btn"));
        Login.click();

我也试过这个 xpath 但还是不行

WebElement Login = driver.findElement(By.xpath("/html/body/div[1]/table/tbody/tr/td[2]/div/div[5]/button"));

【问题讨论】:

  • 你到底尝试了什么?面临哪些问题?
  • 因为我使用的是 IE,所以没有工具可以为它获取 xpath,所以如果你可以给我 xpath,因为我也无法获得它,我尝试使用它的类来定位按钮,但是什么都没有发生
  • 再次,请分享您的代码。之后我们可能会在这里提供帮助
  • 代码添加到问题中。
  • 什么是http://10.67.0.230?这似乎是您的本地地址,对吗?

标签: java selenium xpath css-selectors webdriverwait


【解决方案1】:

想要的元素是Angular元素,所以要在元素上click()你需要诱导WebDriverWaitelementToBeClickable(),你可以使用以下locator strategies之一:

  • cssSelector

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.btn-primary.login-btn[ng-click^='login'] > label.ng-binding"))).click();
    
  • xpath

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn btn-primary login-btn' and starts-with(@ng-click, 'login')]/label[@class='ng-binding' and text()='Login']"))).click();
    

【讨论】:

  • 仍然没有点击,我尝试了“Password.sendKeys("ezEqL7?Ss=g");"下的两行
  • 仍然没有点击是什么意思?代码解决方案是点击按钮元素,文字为Login,与Username.sendKeys();Password.sendKeys("ezEqL7?Ss=g");无关。
  • 我的意思是登录按钮还没有按下。
猜你喜欢
  • 1970-01-01
  • 2013-05-07
  • 1970-01-01
  • 2016-01-30
  • 1970-01-01
  • 2012-08-23
  • 2018-08-01
  • 1970-01-01
  • 2021-03-29
相关资源
最近更新 更多