Reputation: 55
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");
}}
Upvotes: 1
Views: 825
Reputation: 287
Try a different selector. I wouldn't be a fan of the css selector.
Try this -
Then use the driver to find the element by xpath. For an example on this page e.g. 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]/section/div[2]/a[1]"));
element.click();
Upvotes: 0
Reputation: 55
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?
Upvotes: 0
Reputation: 4623
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();
Upvotes: 1