简体   繁体   English

无法在 Selenium Webdriver 中通过 Java 中的任何命令定位按钮

[英]Can't locate a button by any command in Java in Selenium Webdriver

I tried different commands and none of them works.我尝试了不同的命令,但没有一个有效。 I want to find and click "Wykup składkę" button.我想找到并单击“Wykup składkę”按钮。 I'm working on: - Firefox 45.3.0 esr - selenium webdriver 2.53.0 - TestNG我正在开发: - Firefox 45.3.0 esr - selenium webdriver 2.53.0 - TestNG

Here's html code:这是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>

And my test script还有我的测试脚本

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"); 
      }}

You should wait for the element to be present on screen.您应该等待元素出现在屏幕上。 Its taking some time for By.id("username") to be present on screen and you are trying to access that before its present. By.id("username")出现在屏幕上需要一些时间,而您正试图在它出现之前访问它。

You can use the following code :您可以使用以下代码:

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));

Edit : To click on <a class="btn btn-sheer btn-card" href="#/feetable">Wykup składkę</a>编辑:点击<a class="btn btn-sheer btn-card" href="#/feetable">Wykup składkę</a>

You can try :你可以试试 :

 driver.findElement(By.cssSelector("a.btn.btn-sheer.btn-card")).click();

Try a different selector.尝试不同的选择器。 I wouldn't be a fan of the css selector.我不会是 css 选择器的粉丝。

Try this -尝试这个 -

  1. Right click on the element on the page that you want to select.右键单击页面上要选择的元素。
  2. Click on inspect element单击检查元素
  3. When the HTML is highlighted for that element, right click on html and click copy --> xpath .当该元素的 HTML 突出显示时,右键单击 html 并单击复制 --> xpath 。
  4. Then use the driver to find the element by xpath.然后使用驱动程序通过 xpath 查找元素。 For an example on this page eg the add comment button this would look like对于此页面上的示例,例如添加评论按钮,这看起来像

    driver.findElement(By.xpath("//*[@id='add-comment-39636086']/table/tbody/tr[1]/td[2]/input")); driver.findElement(By.xpath("//*[@id='add-comment-39636086']/table/tbody/tr[1]/td[2]/input"));

Edit So with the button xpath you provided this is how I would write it:使用您提供的按钮 xpath 进行编辑,这就是我编写它的方式:

WebElement element = driver.findElement(By.xpath("/html/body/app/main/dashboard/section/div/div[1]/div[1]/sect‌​ion/div[2]/a[1]"));
element.click();

I found a solution to my problem我找到了解决我的问题的方法

driver.findElement(By.cssSelector("div.button-container [href='#/feetable']")).click();

BUT,但,

I wanted to locate and click with this method another button and it DOESN't work.我想用这种方法定位并单击另一个按钮,但它不起作用。 What kind of dark magic is it?这是什么黑魔法?

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

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