无法通过 Selenium Webdriver Java 中的任何命令找到按钮
Can't locate a button by any command in Java in Selenium Webdriver
我尝试了不同的命令,其中 none 行得通。我想找到并单击 "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")
需要一些时间才能出现在屏幕上,而您正试图在它出现之前访问它。
You can use the following code :
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();
尝试不同的 selector。我不会成为 css selector 的粉丝。
试试这个 -
- 右键单击页面上您想要 select 的元素。
- 单击检查元素
- 当该元素的 HTML 突出显示时,右键单击 html 并单击复制 --> xpath 。
然后使用驱动程序通过xpath查找元素。对于此页面上的示例,例如添加评论按钮看起来像
driver.findElement(By.xpath("//*[@id='add-comment-39636086']/table/tbody/tr[1]/td[2]/输入"));
编辑
因此,对于您提供的按钮 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();
但是,
我想用这种方法找到并单击另一个按钮,但它不起作用。这是什么黑魔法?
我尝试了不同的命令,其中 none 行得通。我想找到并单击 "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")
需要一些时间才能出现在屏幕上,而您正试图在它出现之前访问它。
You can use the following code :
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();
尝试不同的 selector。我不会成为 css selector 的粉丝。
试试这个 -
- 右键单击页面上您想要 select 的元素。
- 单击检查元素
- 当该元素的 HTML 突出显示时,右键单击 html 并单击复制 --> xpath 。
然后使用驱动程序通过xpath查找元素。对于此页面上的示例,例如添加评论按钮看起来像
driver.findElement(By.xpath("//*[@id='add-comment-39636086']/table/tbody/tr[1]/td[2]/输入"));
编辑 因此,对于您提供的按钮 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();
但是,
我想用这种方法找到并单击另一个按钮,但它不起作用。这是什么黑魔法?