【问题标题】:Selenium button click does not workSelenium 按钮单击不起作用
【发布时间】:2016-01-20 07:37:08
【问题描述】:

我正在开发具有用户身份验证模块的cloudbase 项目。可以使用用户凭据对用户进行身份验证,也可以使用OAuth 身份验证。

我正在使用 Selenium 自动执行此操作,但是当我尝试单击“登录”按钮时它不起作用,

    @BeforeTest
    public void setUp() 
    {
        driver = new FirefoxDriver();
        driver.get("Application URL");
        driver.manage().window().maximize();

    }

    @Test
    public void enterCredentials() 
    {
        driver.findElement(By.id("cred_userid_inputtext")).sendKeys("email address");
        driver.findElement(By.id("cred_password_inputtext")).sendKeys("password");
        driver.findElement(By.id("cred_sign_in_button")).click();
}

我已经尝试过使用sendKeys(Keys.ENTER)sendKeys(Keys.RETURN)

也尝试使用操作

{
WebElement signIn_button = driver.findElement(By.id("cred_sign_in_button"))

Actions enterSignIn = new Actions(driver);
enterSignIn.moveToElement(signIn_button);
enterSignIn.click();
enterSignIn.perform();
}

【问题讨论】:

  • 你试过定位元素吗?根据您的 xpath,可能有多个元素可用。
  • 你有什么错误吗?
  • 我已经尝试过使用 XPath 以及其他定位器。
  • 没有收到任何错误,唯一的问题是,“点击”不起作用。它找到“登录”按钮并将焦点转移到它上,但不会在主页上登陆用户。

标签: java selenium selenium-firefoxdriver


【解决方案1】:

出于某种原因,在某些页面上,Firefox 似乎需要对 Click() 进行特殊处理。我以这种方式解决了它(在 C# 中,但 Java 应该类似):

// special workaround for the FirefoxDriver
var actions = new Actions(driver);

actions.MoveToElement(element);

ToolBox.DisableTimeout(testParams);
actions.Release().Build().TryPerform();
ToolBox.EnableTimeout(testParams);

actions.MoveToElement(element);
actions.Click().Build().Perform();

说明:我在调用 Click() 之前显式调用了 Release()。有时它是必要的,有时它不是。如果 没有 必要,那么调用 Release() 最终会等待隐式超时(如果有的话)结束,然后引发异常。这就是为什么我在调用 Release() 时暂时禁用超时,以及为什么我将它包装在 TryPerform() 方法中以忽略异常。看看我的 TryPerform() 方法:

public static bool TryPerform(this IAction action)
{
    try
    {
        action.Perform();
    }
    catch (Exception)
    {
        return false;
    }

    return true;
}

我知道,Java 中没有扩展方法,但您可能可以类似地解决这个问题。

【讨论】:

    【解决方案2】:

    点击前睡眠有时会正确模拟点击。喜欢

    Thread.sleep(3000);
        driver.findElement(By.id("cred_sign_in_button")).click();
    

    如果以上一个不起作用,因为您已经尝试过所有定位器和操作,所以请尝试使用 java 脚本执行器

    WebElement element = driver.findElement(By.id("cred_sign_in_button"));
        JavascriptExecutor executor = (JavascriptExecutor)driver;
        executor.executeScript("arguments[0].click();", element);
    

    谢谢你, 壁画

    【讨论】:

      【解决方案3】:

      试试这个,

      WebElement element = driver.findElement(By.id("cred_sign_in_button"));
      JavascriptExecutor executor = (JavascriptExecutor)driver;
      [enter link description here][1]executor.executeScript("arguments[0].click();", element);
      

      访问Here

      【讨论】:

        【解决方案4】:

        您可以尝试使用 expected conditions 显式等待

        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.elementToBeClickable(By.id("cred_sign_in_button"))).click();
        

        【讨论】:

          猜你喜欢
          • 2022-01-19
          • 1970-01-01
          • 2017-05-23
          • 2017-08-18
          • 1970-01-01
          • 1970-01-01
          • 2019-05-16
          • 1970-01-01
          • 2022-08-02
          相关资源
          最近更新 更多