astrakid932
astrakid932

Reputation: 3405

Select elements using Selenium Webdriver PHP?

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

Upvotes: 2

Views: 6051

Answers (3)

Heliezer Garcia
Heliezer Garcia

Reputation: 1

Correct Code is.

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

Upvotes: 0

Vanessa Deagan
Vanessa Deagan

Reputation: 427

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)?

Upvotes: 1

JimEvans
JimEvans

Reputation: 27496

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();
  }

Upvotes: 3

Related Questions