Home > Software engineering >  How to fix "Method Call Expected" using java and selenium when using XPATH to click a butt
How to fix "Method Call Expected" using java and selenium when using XPATH to click a butt

Time:04-25

I am pretty new to all things selenium and I am having trouble right now figuring out how to click on a button (technically a link) using XPATH to search for the correct one.

My Imports are these:

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

And my code is here

    System.setProperty("webdriver.chrome.driver", "C:\\selenium-java-2.35.0\\chromedriver_win32_2.2\\chromedriver.exe");

    WebDriver driver = new ChromeDriver();
    driver.get("https://LINK_OF_THE_WEBSITE");
    WebElement button = WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.ByXPath("//a[@class='btn' @href'LINK_OF_THE_BUTTON']"))).click();

    driver.close();

At the moment I have 2 errors being displayed in my IDE, both tell me "Method call expected." One of them is on WebDriverWait(driver, 10) and the other is on By.ByXPath("...")

I've looked at the JavaDocs for both of these Objects and I feel like I am calling them correctly but I am still getting these errors.

I'm not sure if the method I am using is the best way to go about this. The button doesn't have an ID, only a class and a href value and it is a sign-in button with the text "Sign In". Any help is appreciated!

CodePudding user response:

To click on the link/button you need to identify the element with correct locator.

Use below xpath

//*[@class='btn' and text()='Sign In'] 

Ideally your code will be like

WebElement button=new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@class='btn' and text()='Sign In']")));
button.click();
  • Related