[英]Can't locate a button by any command in Java in Selenium Webdriver
我嘗試了不同的命令,但沒有一個有效。 我想找到並單擊“Wykup składkę”按鈕。 我正在開發: - Firefox 45.3.0 esr - selenium webdriver 2.53.0 - TestNG
這是html代碼:
<div class="row">
<div class="col-md-6">
<section class="card skladki">
<h2> Składki </h2>
<div class="card-content">
<!--template bindings={}-->
</div>
<div class="button-container text-xs-center">
<a class="btn btn-sheer btn-card" href="#/feetable">Wykup składkę</a>
<!--template bindings={}--><a class="btn btn-sheer btn-card" href="#/fees-list">Lista składek</a>
</div>
</section>
</div>
還有我的測試腳本
package testy;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class wykup_skladek_olatest {
public WebDriver driver;
@BeforeMethod
public void beforeMethod() {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.navigate().to("http://dev.wedkarz.pzw.pl/#/login");}
@Test
public void f() throws InterruptedException {
driver.findElement(By.id("username")).sendKeys("****");
driver.findElement(By.id("password")).sendKeys("****");
driver.findElement(By.tagName("button")).click();
driver.navigate().to("http://dev.wedkarz.pzw.pl/#/login");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.partialLinkText("Wykup")).click();
}
@AfterMethod
public void afterMethod() throws InterruptedException {
driver.quit();
System.out.println("Wykupowanie składki - Test zakończony powodzeniem");
}}
您應該等待元素出現在屏幕上。 By.id("username")
出現在屏幕上需要一些時間,而您正試圖在它出現之前訪問它。
您可以使用以下代碼:
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));
編輯:點擊<a class="btn btn-sheer btn-card" href="#/feetable">Wykup składkę</a>
你可以試試 :
driver.findElement(By.cssSelector("a.btn.btn-sheer.btn-card")).click();
嘗試不同的選擇器。 我不會是 css 選擇器的粉絲。
嘗試這個 -
然后使用驅動程序通過 xpath 查找元素。 對於此頁面上的示例,例如添加評論按鈕,這看起來像
driver.findElement(By.xpath("//*[@id='add-comment-39636086']/table/tbody/tr[1]/td[2]/input"));
使用您提供的按鈕 xpath 進行編輯,這就是我編寫它的方式:
WebElement element = driver.findElement(By.xpath("/html/body/app/main/dashboard/section/div/div[1]/div[1]/section/div[2]/a[1]"));
element.click();
我找到了解決我的問題的方法
driver.findElement(By.cssSelector("div.button-container [href='#/feetable']")).click();
但,
我想用這種方法定位並單擊另一個按鈕,但它不起作用。 這是什么黑魔法?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.