簡體   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