Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
4.2k views
by Contributing Tester (22 points)
retagged by

Hi,

Reference the question https://softwaretestingboard.com/qna/3115/able-get-correct-xpath-click-application-selenium-webdriver

Now After using explicit wait, selenium webdriver can locate the element in application by selenium webdriver. Just one query, I have used POM to generate framework and given every locator in java function . The xpath(//*[@id=\"layer_cart\"]/div[1]/div[2]/div[4]/a/span) of element is stored in function Checkout_Page1. Please see below function.  

public static WebElement Checkout_Page1(ChromeDriver driver) {    
        element= driver.findElement(By.xpath("//*[@id=\"layer_cart\"]/div[1]/div[2]/div[4]/a/span"));
        return element;
    }

If i call this function in wait until condition,  then it gives error like"The method xpath(String) in the type By is not applicable for the arguments (WebElement)". The below command, I have used:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath((Checkout_Process_Page.Checkout_Page1(driver)))));

If i used xpath in wait until statement then it is working. Please see below command:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(("//*[@id=\"layer_cart\"]/div[1]/div[2]/div[4]/a/span"))));

As per error message, what i understand that i can not pass WebElement in wait.until statement, It should be string not WebElement. 
Please let me know.

Regards

Samta
 

1 Answer

0 like 0 dislike
by The go-to Tester (142 points)

Yes, you guessed it right. If you are using By, you need to pass String and not WebElement.

To resolve your error, you can rewrite the same code as below.

wait.until( ExpectedConditions.visibilityOfElementLocated( Checkout_Process_Page.Checkout_Page1( driver)));

by Contributing Tester (22 points)
Hi Sandeep,

I have tried as your suggestion below :

wait.until( ExpectedConditions.visibilityOfElementLocated( Checkout_Process_Page.Checkout_Page1( driver)));

 I am getting error message"The method visibilityOfElementLocated(By) in the type ExpectedConditions is not applicable for the arguments (WebElement)".

I think i can not use WebElement with wait.until( ExpectedConditions.visibilityOfElementLocated function, Only we can use only string Xpath.

 Please suggest me for this
by Master (1.2k points)
You can rewrite the function as below.

public static By Checkout_Page1(ChromeDriver driver) {    
        return By.xpath("//*[@id=\"layer_cart\"]/div[1]/div[2]/div[4]/a/span"); //This will return By
 }
...