【问题标题】:Selenium, PHPUnit - click on link using xpathSelenium, PHPUnit - 点击使用 xpath 的链接
【发布时间】:2014-07-06 12:07:01
【问题描述】:

我想使用 XPath 单击链接,但找不到任何解决方案。 这是xpath

//td[text()='".$Variable."']/../td/div/a"

xpath 正在工作,它突出显示正确的元素。但我无法点击它。 整个语法在这里

$this->click("xpath=//td[text()='".$Variable."']/../td/div/a");

这是我在尝试运行测试时收到的错误消息。

PHPUnit_Extensions_Selenium2TestCase_WebDriverException: java.lang.String cannot be cast to java.lang.Long

我读了一些文章,但大多数都是使用 java 的。我想要这个在 PHP 中。

【问题讨论】:

    标签: php selenium xpath click phpunit


    【解决方案1】:

    顺便说一句,这里还有一个例子。 可能更适合你:

    <?php
    class GitHubTest extends PHPUnit_Framework_TestCase {
    
        /**
         * @var \RemoteWebDriver
         */
        protected $webDriver;
    
        public function setUp()
        {
            $capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => 'firefox');
            $this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities);
        }
    
        public function tearDown()
        {
            $this->webDriver->close();
        }
    
        public function testSearch()
        {
            $this->webDriver->get($this->url);
            // find search field by its id
            $search = $this->webDriver->findElement(WebDriverBy::id('js-command-bar-field'));
            $search->click();
    
            $webElement = $this->webDriver->findElement(WebDriverBy::xpath('//*[@property="abracadabra"]'));
        }    
    }
    ?>
    

    所以你可以看到元素是如何使用 xPath 定位的。 这个例子取自Working with PHPUnit and Selenium Webdriver。我想你也会发现它很有帮助。

    【讨论】:

      【解决方案2】:

      我曾经和Facebook php-WebDriver library一起工作过。

      这个解决方案对我来说效果很好:

      $loginButtonCSS="#loginbutton";
      
      $submitButton=driver->findElement(WebDriverBy::cssSelector(loginButtonCSS));
      $submitButton->click();
      

      使用 xPath 的替代方法:

      // NOTE: try to omit symbols  '  in your xPath
      $elementXpath= "xpath=//td[text()=".$Variable."]/../td/div/a"; 
      $webElement= driver->findElement(WebDriverBy::xpath($elementXpath));
      $webElement->click();
      

      希望对你有所帮助。

      【讨论】:

      • 您好,感谢您的回答。但也有一个问题。可能是因为我没有正确安装 facebook php-webdriver。这是我现在要运行此测试时的错误消息。 InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression xpath=//td[text()=XXX180881]/../td/d e following error: TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted to the desired
      • 等等,如果我理解的很好,我必须根据 facebook php-webdriver 表达式重写我的代码中的所有内容?
      • 首先,尝试检查您是否正确计算出元素的xPath:即在fireBug中检查此xPath:gyazo.com/61504eb631451c457112be347438f123;其次,检查您是否已正确包含所有必要的库,并且您能够执行 webDriver 方法调用
      【解决方案3】:

      如果你使用 PHPUnit_Extensions_Selenium2TestCase

      将表达式存储在变量中

      $xpath="//input[@value='text']";
      $this->byXPath($xpath)->click();
      

      【讨论】:

        【解决方案4】:

        似乎您没有扩展正确的类。似乎您正在尝试使用 selenium rc。如果你想使用,你应该扩展 PHPUnit_Extensions_SeleniumTestCase

        $this->click("//someXpath");
        

        如果您使用 php-webdriver,则必须扩展 PHPUnit_Framework_TestCase 并包含 php facebook webdriver(或您正在使用的任何其他 php-webdriver 库)。请记住,selenium RC 测试不适用于 webdriver,反之亦然。如您所见,它们有不同的方式来识别元素以及与浏览器本身进行交互。有关 selenium 和 phpunit 的更多信息,Here.

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-05-02
          • 2012-06-22
          • 2021-07-11
          • 2011-09-10
          • 2020-09-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多