简体   繁体   中英

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. I'm working on: - Firefox 45.3.0 esr - selenium webdriver 2.53.0 - TestNG

Here's html code:

    <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.

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>

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.

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 .
  4. Then use the driver to find the element by 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"));

Edit So with the button xpath you provided this is how I would write it:

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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