簡體   English   中英

如何使用帶有 Java 的 Selenium WebDriver 單擊按鈕?

[英]How can I click on a button using Selenium WebDriver with Java?

以下是按鈕的 HTML 代碼:

<span>
<button class="buttonLargeAlt" onclick="javascript:submitCheckout(this.form);"type="submit">Checkout</button>
</span>

我試過driver.findElement(By.xpath("//span[contains(.,'Checkout')]")).click();

它不工作...

還有其他想法嗎? 頁面上有 2 個同名按鈕。

driver.submit()

應該管用。 如果 DOM 中按鈕的順序始終相同,這也應該有效:

driver.findElements(By.className("buttonLargeAlt")).get(0).click();

如果它是您頁面上的第一個 buttonLargeAlt 按鈕。

嘗試:

//span/button[text()='Checkout' and @class='buttonLargeAlt']

要么

//span/button[text()='Checkout'][1]

此外,如果您知道需要單擊 2 個按鈕中的哪一個,您可以嘗試:

//span/button[text()='Checkout'][1]

其中[1]是第一個帶有'Checkout'文本的按鈕

以下應該工作:

driver.findElement(By.className("buttonLargeAlt")).click();
driver.findElement(By.xpath("//button[contains(@class='buttonLargeAlt')]")).click();
driver.findElement(By.xpath("//button[@class='buttonLargeAlt']")).click();
    You can achieve this by using XPath with html input element id or by name
    //1. By XPath indexing option:  
    WebElement loginButtonId = 
    driver.findElement(By.xpath("//*[@id='login']"));
    //Xpath of login button i have get For firefox browser

    loginButtonId.click();

    I hope this work for you

我有添加附件按鈕:

我用這個代碼試過:

driver.findElement(By.xpath("//*[@id=\"attachments\"]/div/div/img")).sendKeys("C:\\Users\\NayazPasha\\Desktop\\Ndin selenium Testing outputs\\Collab Schedule onclick.png");

該 XPath 只會獲取跨度,而不會是物理按鈕。

在這里工作得很好:

//span[contains(.,'Checkout')]/button

或 By.CssSelector:

button.buttonLargeAlt

如果仍然不起作用,請解釋更多。 它在 iFrame 中嗎? Selenium 會出現什么錯誤?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM