2

I have a number of page elements that I want to store in a variable and loop through using Selenium Webdriver PHP.

For example:

< cite > Name 1 < /cite >
< cite > Name 2 < /cite >
<cite > Name 3< /cite >

I am using the following code, but it doest give me the results from above(i.e. Name 1) etc. How do I grab the text from the element using Selenium Webdriver.

$users = $driver->findElements(
  WebDriverBy::xpath('//cite')
)->getText();
foreach($users as $u)
         {
             echo $u;
         }

I am using Selenium Webdriver Facebook wrapper

4
  • instead of findElements() you need to use findElement() to access getText() Commented Oct 22, 2013 at 14:27
  • yes but that only gets me the first element, it does not get all elements as an array / object that I can loop... how would I get all elements in the variable $users? Commented Oct 22, 2013 at 14:44
  • since I need to get all the text from multiple elements, any ideas? is there another method that grabs all elements ? Commented Oct 22, 2013 at 15:14
  • anyone got any ideas? Commented Oct 22, 2013 at 15:14

3 Answers 3

3

I don't really know PHP, but in Java, you'd do something similar to:

List<WebElement> elements = driver.findElements(By.xpath("//cite"));
for (WebElement element: elements) {
  System.out.println(element.getText());
}

Given that, I'd assume that the PHP equivalent would be something like this:

$users = $driver->findElements(WebDriverBy::xpath('//cite'));
foreach($users as $u)
  {
    echo $u->getText();
  }
Sign up to request clarification or add additional context in comments.

2 Comments

Tried it but gives me Fatal error: Call to a member function getText() on a non-object
Again, I'll be the first to admit that I know next to nothing about PHP, nor about the bindings you're using. The bottom line is that your initial code is flawed. findElements() should be returning you a list of objects, and iterating over the list of returned objects, you should be able to call getText() on each one. You'll need to figure out what the PHP bindings you're using are doing and what the structure is of the objects findElements() returns.
1

JimEvan's answer is correct. If you're getting a "non-object" type error, you should check to make sure the findElements() call is actually returning something:

$users = $driver->findElements(WebDriverBy::xpath('//cite'));
fwrite(STDOUT, "Number of users: " . count($users));

Perhaps it has something to do with the space characters in your element tags (just a guess)?

Comments

0

Correct Code is.

$users = $driver->findElements(WebDriverBy::xpath('//cite')); foreach($users as $u) {echo $u->getText();}

1 Comment

Could you elaborate more?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.