如何在网页上单击具有href属性的按钮

时间:2019-08-07 20:05:18

标签: selenium webdriver

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

审判1:

let str = "this pull request has 08/09 discussions 0/09"
var regex = /\d+\/\d+(?=\s*discussions)/g
var matched = str.match(regex)

console.log(matched)

审判2:

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

1 个答案:

答案 0 :(得分:1)

如果元素同时包含 href 属性和 text 登录,则可以使用以下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();