首页 > 解决方案 > 如何在网页上单击具有 href 属性的按钮

问题描述

我已使用以下代码单击登录按钮,但它不起作用:

试验一:

driver.findElement(By.xpath("//a[text()='https://www.luxproflashlights.com/customer/account/login/referer/aHR0cHM6Ly93d3cubHV4cHJvZmxhc2hsaWdodHMuY29tLw%2C%2C/']")).click(); 

试验二:

driver.findElement(By.linkText("Log In")).click();

标签: seleniumwebdriver

解决方案


If the element contains both the href attribute and the text Log In you can use either of the following Locator Strategies:

  • partialLinkText:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Log In"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(@href, 'luxproflashlights') and contains(., 'Log In')]"))).click();
    

推荐阅读