我有一个带提交按钮的表单
<input id="myid" type="submit" value="My button"></input>
我尝试了很多不同的方法来点击它,但它不起作用。为什么?我使用Selenium Driver和PhantomJS浏览器。 我尝试了什么:
$page = $this->getSession()->getPage();
$page->find('xpath', '//*[@id="myid"]')->click();
$page->find('xpath', '//*[@id="myid"]')->doubleClick();
$page->find('xpath', '//*[@id="myid"]')->press();
$page->find('css', '#myid')->click();
$page->find('css', '#myid')->doubleClick();
$page->find('css', '#myid')->press();
$this->getMinkContext()->pressButton('myid');
答案 0 :(得分:2)
我们使用这样的函数:
/**
* @Then /^(?:|I )click (?:on |)(?:|the )"([^"]*)"(?:|.*)$/
*/
public
function iClickOn($arg1)
{
$findName = $this->getSession()->getPage()->find("css", $arg1);
if (!$findName) {
throw new Exception($arg1 . " could not be found");
} else {
$findName->click();
}
}
用法1:
Scenario: Test Click
Given I go to "<url>"
And I click the "#myId" button - to reveal a hidden element
Then I should see an "<url2>" element
用法2:
Scenario: Test Click
Given I go to "<url>"
And I click the "#myId" link
Then I should be on "<url2>"
如果这对你不起作用,那可能是因为路上有一个元素?您想要真正点击的按钮是什么?
如果它不是,那么也许这对你也有帮助:
/**
* @When I scroll :elementId into view
*/
public function scrollIntoView($elementId) {
$function = <<<JS
(function(){
var elem = document.getElementById("$elementId");
elem.scrollIntoView(false);
})()
JS;
try {
$this->getSession()->executeScript($function);
}
catch(Exception $e) {
throw new \Exception("ScrollIntoView failed");
}
}
就我个人而言,我还没有使用最终功能,但是同样的功能帮助了一个人在我之前看到的其中一个帖子。