简体   繁体   中英

Uncaught Facebook\WebDriver\Exception\UnknownCommandException: POST ../element//click while clicking Google Search button using Selenium and IE 8

I'm having trouble with a click() function in my php code. It always throws this exception:

Fatal error: Uncaught Facebook\\WebDriver\\Exception\\UnknownCommandException: POST /session/f3cffab9-71ad-4e0a-baab-4a46d807ce3d/element//click

I'm running it on:

  • windows 2008 server x64
  • selenium standalone server 3.9.1
  • PHP 7.1

But it doesn't work with 7.0 or 7.2 either. I'm using the newest facebook webdriver and the newest IEdriver as well.

The code I'm trying to run is:

<?php

use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverDimension;
use Facebook\WebDriver\WebDriverExpectedCondition;
use Facebook\WebDriver\WebDriverPoint;

require_once __DIR__ . '/vendor/autoload.php';

$host = 'http://localhost:4444/wd/hub';

$driver = RemoteWebDriver::create($host, DesiredCapabilities::InternetExplorer());

// Set size
$driver->manage()->window()->setPosition(new WebDriverPoint(0,0));
$driver->manage()->window()->maximize();
$driver->get("http://www.google.com");
sleep(1);

$driver->findElement(Facebook\WebDriver\WebDriverBy::name('q'))->click();
sleep(1);
$driver->findElement(Facebook\WebDriver\WebDriverBy::name('q'))->sendKeys('test');
sleep(1);

// Click the search button
$driver->findElement(Facebook\WebDriver\WebDriverBy::name('btnK'))->click();
$driver->quit();

?>

If I'm understanding everything correctly it should go to google, select the search bar and put the string "test" in there. Then it should select the submit button and submit the form. I've tried different sites as well as different commands and it seems like I can do everything but click and sendKeys . I also tried it with the newest Firefox and geckodriver and got the same result.

This error message...

Fatal error: Uncaught Facebook\WebDriver\Exception\UnknownCommandException: POST /session/f3cffab9-71ad-4e0a-baab-4a46d807ce3d/element//click

...implies that the click() method failed.

If you examine the HTML DOM of Google Home Page through development tools, you will observe the Locator Strategy which you have used as:

Facebook\WebDriver\WebDriverBy::name('btnK')

doesn't identifies the Google Search button uniquely but it identifies 2 different elements.

Snapshot:

谷歌搜索按钮

As per the rendering of DOM Tree the desired element doesn't receives the click.


Solution

As an alternative you can use either of the following Locator Strategies :

  • cssSelector :

     div[class]:not([jsname])>center>input[name='btnK']
  • xpath :

     //div[@class and not(@jsname)]/center/input[@name='btnK']

PS : Consider updating Selenium to current levels Version 3.141.59 .

As the error message as shown below states the WebDriver command is not known:

Fatal error: Uncaught Facebook\\WebDriver\\Exception\\UnknownCommandException: POST /session/f3cffab9-71ad-4e0a-baab-4a46d807ce3d/element//click

When you correctly check the end-point for the POST request you will notice that there is a double slash. Instead it should be "element/click".

So this is a bug in the Facebook's webdriver client, and as such will fail with any driver. Are you really using the newest version of the client? Checking the current source on Github it seems to be all fine, and the code hasn't been changed for nearly 3 years.

Well I solved my issue, it turned out to be little bit more complex. There was no problem with the click() function itself but rather with the findElement() function. There was a problem with the library itself. It expects ELEMENT as an index in $raw_element while the webdriver itself returns element-with-some-id as an index. Editing the library as follows...

public function findElement(WebDriverBy $by)
    {
        $params = ['using' => $by->getMechanism(), 'value' => $by->getValue()];
        $raw_element = $this->execute(
            DriverCommand::FIND_ELEMENT,
            $params
        );
        //my code
        return $this->newElement(reset($raw_element));
        //end of my code
    }

...solved the problem in this case, however it should be noted that every function that relies on this $raw_element variable will need to be edited in order for it to work properly. Thanks everyone for helping me out on this one.

UPDATE

Now that I fully understand the problem, it is caused by webdriver using W3C protocol. If you want to evade this issue eigher use chrome and chromedriver, or downgrade your driver to an old version (back in 2017).

就我而言,解决问题的方法只是将旧的facebook/webdriver (版本 1.6.0)升级到最新的php-webdriver/webdriver (版本 1.8.2)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM