1

How do you use a relative xpath inside of a wait.until?

For example, the below will work:

elementA.FindElement(By.Xpath(.//div));

However, putting a 'FindElement' inside of the wait.Until() defeats the purpose of the wait.Until() ... the below code is bad

Wait.Until(elementA.FindElement(By.Xpath(.//div)));

The wait will no longer occur because the FindElement is inside the wait and inner most nested code gets executed first. So how do you do this? At first I thought I could get the xpath of the ElementA and then append that to the front of the relative XPath (which probably slows down everything) but backing out an XPath from an IWebElement as shown below doesn't seem to be very reliable.

https://groups.google.com/forum/#!topic/selenium-users/kvGLSzl_GQE

    protected String getXPath() { 
    String jscript = "function getPathTo(node) {" + 
        "  var stack = [];" + 
        "  while(node.parentNode !== null) {" + 
        "    stack.unshift(node.tagName);" + 
        "    node = node.parentNode;" + 
        "  }" + 
        "  return stack.join('/');" + 
        "}" + 
        "return getPathTo(arguments[0]);"; 
    return (String) driver.executeScript(jscript, webElement); 
} 
0

Question:

Does the solution described below sound like overkill. Is it a bad idea to use two wait.Until in a row or is this how others have achieved the same task?


The below code works successfully (find clickable element):

IWebElement element; // some element already initialized
System.TimeSpan timeToWait = new TimeSpan(0, 0, 20);
WebDriverWait wait = new WebDriverWait(DriverContext.Driver, timeToWait);
By by = By.XPath("div[contains(@id,'newButton')]");

IWebElement existingElement = wait.Until(driver => element.FindElement(by));

// AND THEN the important step to wait until the element you found is clickable
IWebElement clickableElement = wait.Until(ExpectedConditions.ElementToBeClickable(existingElement));

Found this form of the wait.Until described in the below page: https://github.com/SeleniumHQ/selenium/blob/master/dotnet/src/webdriver/Support/WebDriverWait.cs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.