繁体   English   中英

Selenium WebDriver 如何获取 WebElement 的按钮

[英]Selenium WebDriver how to get Button for WebElement

我从网站登录获得了以下 HTML 代码。

<button type="submit" class="btn ow-btn-primary btn-block-xs pull-right">
     <span class="glyphicon glyphicon-log-in"></span>
     "Log On"
</button>

我希望能够访问 selenium 中的按钮。 我需要什么代码才能在 java 中调用它?

使用以下xpath来识别 Button 元素。

//button[@class='btn ow-btn-primary btn-block-xs pull-right' and contains(.,'Log On')]

诱导WebDriverWaitelementToBeClickable ()

WebDriverWait wait = new WebDriverWait(driver, 10);         
WebElement element=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn ow-btn-primary btn-block-xs pull-right' and contains(.,'Log On')]")));
element.click();

您可以尝试以下语句来定位元素

WebElement submitBtn = driver.findElement(By.xpath("//button[contains(text(),'Log On']"));

WebDriverWait 等待 = 新的 WebDriverWait(驱动程序, 20);
WebElement element=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@type='Submit' and contains(.,'Log On')]"))); 元素.click();

您可能可以复制它的 xPath 并使用以下代码:

WebElement submit_button = driver.findElement(By.xpath("xpath goes here");

如果您想按下按钮,请尝试:

submit_button.click();

暂无
暂无

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

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