Mink无法执行JavaScript

时间:2018-02-16 19:19:01

标签: php selenium-webdriver mink

我尝试使用JS脚本点击元素以避免"其他元素会收到点击"错误。这是我的代码:

$drive = $this->getSession()->getPage();
$element = $drive->find('css', 'input[value^="Enregistrer et C"]');
$this->getSession()->executeScript("arguments[0].click();", $element);

结果我跟随错误:

unknown error: Cannot read property 'click' of undefined
      (Session info: chrome=64.0.3282.140)
      (Driver info: chromedriver=2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881),platform=Linux 4.13.0-32-generic x86_64) (WARNING: The server did not provide any stacktrace information)

我做错了什么?

HTML代码:

<div class="layout-region layout-region-node-footer">

  <div data-drupal-selector="edit-actions" class="form-actions js-form-wrapper form-wrapper" id="edit-actions">

    <div class="dropbutton-wrapper dropbutton-multiple">
      <div class="dropbutton-widget">
        <ul data-drupal-selector="edit-save" class="dropbutton">
          <li class="moderation-state-draft dropbutton-action">
            <input type="submit" name="op" value="Enregistrer et Créer un nouveau brouillon" class="button button--primary js-form-submit form-submit"></li>

更新: 所需的元素在页面上是不可见的,我需要向下滚动才能看到它,另一方面它上面有几个元素,所以简单的selenium点击将返回&#34;其他元素将获得点击&#34 ;错误。所以我决定使用JS scipt来点击元素:

2 个答案:

答案 0 :(得分:0)

根据 HTML ,您已共享 WebElement ,其属性为class AdvancedSearch(ListView): template_name= 'saferdb/AdvancedQuery.html' paginate_by=25 context_object_name = 'contacts' model = Question def get_queryset(self): c = self.request.GET.getlist('opeclass') q = Question.objects.all() ... return queryset def get_context_data(self, **kwargs): context = super(AdvancedSearch, self).get_context_data(**kwargs) context.update({...}) return context 。因此,您可以更细化并更改行:

button button--primary js-form-submit form-submit

致:

$element = $drive->find('css', 'input[value^="Enregistrer et C"]');
  

注意:由于属性包含$element = $drive->find('css', 'input.button.button--primary.js-form-submit.form-submit[name="op"][value^="Enregistrer et C"]'); ,因此您需要为 webelement 引入 WebDriverWait em>在找到它之前在 HTML DOM 中进行渲染。

答案 1 :(得分:0)

以下JS功能帮助了我:

public function clickJS ($element){
    $jsclick = <<<JS
        function getElementByXpath(path) {
            return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    }
    getElementByXpath('$element').click();
JS;
    try {
        $this->getSession()->executeScript($jsclick);
    }
    catch(Exception $e) {
        throw new \Exception($e);
    }
}