0

I have a form with submit button

<input id="myid" type="submit" value="My button"></input>

I tried a lot different ways to click on it, but it doesn't work. Why? I use Selenium Driver with PhantomJS browser. What I tried:

$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');
Pac Rauce
  • 229
  • 1
  • 5
  • 11
  • Do you have any errors? Step was marked as failed or passed? Maybe you have error like: "Element is not clickable at point" and you have different problem ( maybe element have overlaps with other elements and then you can click to it , or element is not visible). Please provide more information. – ChechoroArtem Feb 17 '16 at 12:50

1 Answers1

2

We use a function like this:

    /**
     * @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();
        }
    }

Usage 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

Usage 2:

Scenario: Test Click
Given I go to "<url>"
And I click the "#myId" link
Then I should be on "<url2>"

If this doesn't work for you, then it may be because there is an element in the way? Is the button that you want to click on genuinely there?

If it isn't, then perhaps this will help you too:

  /**
   * @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");
    }
  }

Personally, I haven't used the final function, however the same function helped a person in one of the posts I was viewing before this.

KyleFairns
  • 2,947
  • 1
  • 15
  • 35