How to take data from an object returned by Selenium with php?

Asked

Viewed 404 times

0

I am using Selenium + Chrome Webdriver and php to scrape data from a website and, for that, I need to make a condition with the width of a column of a table of that site. I do it with the following code:

$td3 = $driver ->findElement(WebDriverBy::xpath('/html[1]/body[1]/form[1]/div[2]/div[1]/table[2]/tbody[1]/tr[5]/td[3]')) -> getSize();

Only Selenium returns the size to me in object form. As shown in two shapes below.

With var_dump :

Object(Facebook Webdriver Webdriverdimension)#19 (2) { ["height":"Facebook Webdriver Webdriverdimension":private]=> int(38) ["width":"Facebook Webdriver Webdriverdimension":private]=> int(0) }

With print_r :

Facebook Webdriver Webdriverdimension Object ( [height:Facebook Webdriver Webdriverdimension:private] => 38 [width:Facebook Webdriver Webdriverdimension:private] => 0 )

I only want 0, which is the width of the element. How do I get it?

Addend1: When I try to use the function getWidth(). This error is generated (which I only have access to by the.log php 7 file):

PHP Fatal error: Uncaught Error: Call to Undefined method Facebook Webdriver Remote Remotewebelement::getWidth() in /var/www/html/Inpi/Crawler.php:92 nStack trace: n#0 {main} n thrown in /var/www/html/Inpi/Crawler.php on line 92

Addend2: No css defined in tag for me to use getAttribute('width'). Still I tried and it didn’t work.

  • I’ve tried, but nothing comes back :(

  • I added the code I used at first

1 answer

0


According to the "documentation", the object Facebook\WebDriver\WebDriverDimension has two methods to capture the value of properties:

  • getHeight() to return the height
  • getWidth() Parap returns width

Like the properties height and width are private (only the class has access), then it is necessary to use the above methods to obtain this value, for example:

$td3 = $driver->findElement(WebDriverBy::xpath('/html[1]/body[1]/form[1]/div[2]/div[1]/table[2]/tbody[1]/tr[5]/td[3]'))->getSize();

var_dump($td3->getHeight()); //Output: 38
var_dump($td3->getWidth()); //Output: 0
  • I’ve tried, but nothing comes back

  • @Jessicakeilla edited my answer. I added var_dump. In case you are trying to print this whole with echo, it is necessary to convert it to string before printing.

  • An error occurred while trying to use the above methods?

  • This was found in php 7.0 error.log : >>PHP Fatal error: Uncaught Error: Call to Undefined method Facebook Webdriver Remote Remotewebelement::getWidth() in /var/www/html/Inpi/Crawler.php:92 nStack trace: n#0 {main}n thrown in /var/www/html/Inpi/Crawler.php on line 92

  • I used all kinds of result printing and nothing.

  • @Jessicakeilla you are using the wrong object. I updated my answer.

  • How did I not think of this before? kkk. Thanks @Valdeirpsr

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.