Selenium Element Identification in php

We can identify the web elements using below Element locators in Node.js
  • className
  • tagName
  • name
  • id
  • xpath
  • css
  • linkText
  • partialLinkText
  • WebDriverBy::className()
  • WebDriverBy::tagName()
  • WebDriverBy::name()
  • WebDriverBy::id()
  • WebDriverBy::xpath()
  • WebDriverBy::cssSelector()
  • WebDriverBy::linkText()
  • WebDriverBy::partialLinkText()
findElements method returns an array of elements. This method returns all elements matching given locator. Below example demonstrates how to use findElements method in Selenium in PHP.
 
<?php

class LinkTest extends PHPUnit_Framework_TestCase {

protected $webDriver;

public function setUp()
{
$capabilities = array(WebDriverCapabilityType::BROWSER_NAME => ‘chrome’);
$this->webDriver = RemoteWebDriver::create(‘https://localhost:4444/wd/hub’, $capabilities);
}

public function tearDown()
{

$this->webDriver->quit();
}

public function testLink()
{
$this->webDriver->get(“https://www.softpost.org”);

//Show all links containing selenium on page
$links = $this->webDriver->findElements(WebDriverBy::partialLinkText(‘Selenium’));
foreach($links as $link)
{
echo (“
” . $link->getAttribute(‘href’) . “
”);
}

//find and click Selenium link
$seleniumLink = $this->webDriver->findElement(WebDriverBy::linkText(‘Selenium’));
$seleniumLink->click();

$this->webDriver->close();
}
}

?>

Complex problems, Simple Solutions