带有PHP的Selenium WebDriver在所有元素结果上返回{quot;ELEMENT:0}

带有PHP的Selenium WebDriver在所有元素结果上返回{quot;ELEMENT:0},php,curl,selenium,selenium-webdriver,Php,Curl,Selenium,Selenium Webdriver,我有一个运行在VPS上的Ubuntu服务器,安装了LXDE和Firefox。我还使用以下命令启动了Selenium服务器: java -jar selenium-server-standalone-2.33.0.jar 它现在正在本地主机端口4444上运行和侦听。要通过PHP连接到selenium服务器,我使用以下简单脚本: $url = "http://www.amazon.com"; $dc = array("browserName" => "firefox",

我有一个运行在VPS上的Ubuntu服务器,安装了LXDE和Firefox。我还使用以下命令启动了Selenium服务器:

java -jar selenium-server-standalone-2.33.0.jar
它现在正在本地主机端口4444上运行和侦听。要通过PHP连接到selenium服务器,我使用以下简单脚本:

$url = "http://www.amazon.com";
$dc = array("browserName" => "firefox",
                 "version" => "",
                 "platform" => "ANY",
                 "javascriptEnabled" => True);

// To get the session id
$session_id = get_id($dc);  

// Open 'amazon.com'
$new_map = "/session/".$session_id."/url";
$result = curl_transfer('POST', array('url' => $url), $new_map);

// Wait until the page completely been loaded
$new_map = "/session".$session_id."/timeouts/implicit_wait";
curl_transfer('POST', array('ms' => 7000), $new_map);

// Find an element, here is the Brazil link at the bottom of the page
$new_map = "/session/".$session_id."/element";
$t = curl_transfer('POST', array('using' => 'link text', 'value' => 'Brazil'), $new_map);

print_r($t);


function curl_transfer ($method, $params, $map){
   $path = 'http://localhost:4444/wd/hub'.$map;
   $c = curl_init($path);
   if ($method === 'POST') {

       $encoded_params = json_encode($params);

       curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 30);
       curl_setopt($c, CURLINFO_HEADER_OUT, true);
       curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($c, CURLOPT_HEADER, true);
       curl_setopt($c, CURLOPT_HTTPHEADER, array(
               'Content-Type: application/json;charset=UTF-8',
               'Accept: application/json',
               'Expect:'));
       curl_setopt($c, CURLOPT_POST, 1);
       curl_setopt($c, CURLOPT_POSTFIELDS, $encoded_params);

       $raw_result = curl_exec($c);
       // print_r(curl_getinfo($c)); 
       return (string)$raw_result;
   }
   if ($method === 'GET') {

      curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 30);
      curl_setopt($c, CURLINFO_HEADER_OUT, true);
      curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($c, CURLOPT_HEADER, true);

  $raw_result = curl_exec($c);
      // print_r(curl_getinfo($c)); 
      return (string)$raw_result;
   }
}

function get_id ($dc){
   $result = curl_transfer('POST', array('desiredCapabilities' => $dc), '/session');
   $array_result = explode ("\r\n", $result);
   $id = str_replace("Location: http://localhost:4444/wd/hub/session/", "", $array_result[6]);
   return $id;
}
一切正常,会话正在启动,Firefox被导航到Amazon.com, 但是,每次尝试查找元素时,我都会将其作为返回对象:

{"status":0,"sessionId":"3a0d5939-595e-40d6-88c8-fe6f22a165dd","value":{"ELEMENT":"0"},"state":null,"class":"org.openqa.selenium.remote.Response","hCode":15920450}

似乎根本没有找到元素。这里怎么了?有什么想法吗?

快速查看一下亚马逊的页面来源,就会发现很多iframe…你的元素在其中吗?不要认为它与iframe有任何关系,因为我在另一个网站上使用了它,并得到了相同的响应。这里的亚马逊案例只是一个例子。