0

Using Java with Selenium and Eclipse and Chrome. I was on the page in Chrome I wanted to find an xpath for, so I did an inspect, and then right-click->copy->xpath and it returned the following:

 //*[@id="queueForm"]/div[1]/span[3]/text()[2]

So I did a

 // driver is an instance of ChromeDriver
 String xp = "//*[@id="queueForm"]/div[1]/span[3]/text()[2]";
 WebElement ele = driver.findElement(By.xpath(xp));

and I received an error message that that returned an object and not WebElement. Anyone run into this situation before? The HTML is like

<span class="displayOption" style="float:right;">
        Page <input id="gotoresultpage" type="text" onkeypress="javascript:fncCaptureEnter(this);" size="1" maxlength="4" tabindex="1" value="1" class="navigationMenu" style="width:20px;text-align:right"> of 2
        <input type="button" tabindex="1" value="Go" id="pageGo" name="pageGo" class="navigationButton" onclick="g_objQueueUiController.fncGo();">

        <span class="recordNavigationBack"></span>

        <span class="recordNavigationForwardEnabled" id="pageNext" name="pageNext" onclick="g_objQueueUiController.fncGoToPage('2')"></span>

</span>

You can see text "Page" in there and a bit later "of 2". The page number is in the input box.

So I am trying to get the "of 2" text. I already got the page number from the value of the input box, but not sure how to get the "of 2".

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Tony
  • 1,127
  • 1
  • 18
  • 29

5 Answers5

0

try this

xpath = //span[@class='displayOption']//input[@id='gotoresultpage']

driver.findElement(By.xpath(xpath))).getAttribute("value");
Raj
  • 21
  • 6
  • Thanks. I need the total number of pages though. I was able to figure out the current page number. The value attribute, I believe, would return "1" which is the current page number (1 of 2). Maybe you mean getText() will that work? I think that is what Dimitri said too? – Tony Aug 28 '19 at 12:43
  • 1
    Endeavour to always add some explanation to your answers. This answer is very vague. – Taslim Oseni Aug 28 '19 at 13:24
0

Remove this text() from the end of your XPath expression, the value returned from the WebDriver.find() function must be a Node, not the text.

So you need to amend your code to something like:

//input[@id='gotoresultpage']

example code:

WebElement someElement = driver.findElement(By.xpath("//input[@id='gotoresultpage']"))
String text = someElement.getText();

More information:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • 1
    I will try that. I guess I didn't realize an //input could also have text and not just a value. I wonder why the copy xpath did not offer that? – Tony Aug 28 '19 at 12:46
0

While running Selenium Web-driver, sometimes the page takes a bit more time to load DOM at the back-end and page itself which causes the web-driver to fail a test step. Try using 'Waits' in your code so that it provides a bit more time to load your page and DOM. Try using this code for wait:

WebDriverWait wait = new WebDriverWait(driver, 15); wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));

Or simply use thread.sleep: Thread.sleep(Long Millis, Int nanos);

(Example: Thread.sleep(5000, 50000);this will implement 5 seconds sleep between your test steps)

Also, pls note that auto generated xpaths are dynamic which may update on your next test script execution so be vigilant while selecting a web element to be found through web-driver.

  • I didn't realize sleep took a second parameter, nanos. That is 1/1,000,000,000 of a second. Wow. – Tony Aug 28 '19 at 12:49
0

The text of 2 is a text node, so to extract the text you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using cssSelector:

    System.out.println(((JavascriptExecutor)driver).executeScript('return arguments[0].childNodes[3].textContent;', new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#queueForm>div span.displayOption[style*='right']")))).toString());
    
  • Using xpath:

    System.out.println(((JavascriptExecutor)driver).executeScript('return arguments[0].childNodes[3].textContent;', new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id="queueForm"]/div//span[@class='displayOption' and contains(@style, 'right')]")))).toString());
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

I did the test in following link : https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_form

Here is the HTML

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php" id="form1">
First name: <input type="text" name="fname"><br>
<input type="submit" value="Submit">
</form>

<p>The "Last name" field below is outside the form element, but still part of the form.</p>

Last name: <input type="text" name="lname" form="form1">

</body>
</html>

I got the text "First name" of form

Here my code

driver.switchTo().frame("iframeResult");
driver.findElement(By.id("form1")).getText();

Last line return "First name"