【问题标题】:selenium (phpunit) click on drop down linkselenium (phpunit) 点击下拉链接
【发布时间】:2014-03-17 10:18:54
【问题描述】:

我希望能够使用 selenium 和 phpunit 从下拉菜单中单击链接。我不知道它是如何发生的,任何人都可以向我展示示例或相关教程、帖子或任何可以帮助我弄清楚的东西。

当我尝试点击元素而不将鼠标放在下拉菜单上时,我收到了这个错误:

元素当前不可见,因此可能无法与命令交互 ....

谢谢。

编辑: 当我说“下拉”时,我并不是指常规选择。它更像是弹出窗口 你可以在这里看到这个例子: http://investing.com

看看他们如何构建我希望能够点击“技术”->“斐波那契计算器”的菜单。

【问题讨论】:

    标签: selenium drop-down-menu phpunit mouseover


    【解决方案1】:

    查看这篇文章: Selenium: How to select an option from a select menu?

    你可以找到更多关于这个here的信息

    select(selectLocator, optionLocator)
    
    Arguments:
    
        selectLocator - an element locator identifying a drop-down menu
        optionLocator - an option locator (a label by default)
    
    Select an option from a drop-down using an option locator.
    
    Option locators provide different ways of specifying options of an HTML Select element (e.g. for selecting a specific option, or for asserting that the selected option satisfies a specification). There are several forms of Select Option Locator.
    
        label=labelPattern: matches options based on their labels, i.e. the visible text. (This is the default.)
            label=regexp:^[Oo]ther
        value=valuePattern: matches options based on their values.
            value=other
        id=id: matches options based on their ids.
            id=option1
        index=index: matches an option based on its index (offset from zero).
            index=2
    
    If no option locator prefix is provided, the default behaviour is to match on label.
    

    感谢戴夫·亨特


    我用什么:

    $search13 = $this->webDriver->findElement(WebDriverBy::id('id_of_dropdown_field'));
    $search13->click(); // Clicking on the dropdownfield
    
    $this->webDriver->getKeyboard()->pressKey('ARROW_DOWN'); // Will go down in your dropdown selection )
    
    sleep(1);
    
    $this->webDriver->getKeyboard()->pressKey('ENTER'); // Enter for submitting your selection
    

    编辑: http://www.anitpatel.net/2012/02/25/selenium-webdriver-how-to-click-on-a-hidden-link-or-menu/ 这个用java解释了它,但基本上他所做的是鼠标悬停/悬停并等待。 然后他点击元素。 我不是 Java 天才,但这是一个如何使用它的示例。

    您可以使用:

    string mouseOver(string $locator)
    

    这模拟了用户将鼠标悬停在指定元素上。 http://docs.tadiavo.com/phpunit/www.phpunit.de/pocket_guide/3.1/en/selenium.html

    【讨论】:

    • 谢谢,但请参阅我的编辑,我不需要常规选择选项。 (如果没有人很快得到正确答案,我会将您的答案标记为正确答案)。
    • 嗯,这很烦人,您可以单击菜单,但只有侧面子菜单不会显示在界面中。一个快速的解决方法是转到 URL 本身。 ' investing.com/forex-tools/fibonacci-calculator ' 那么这个。或者您可以点击工具 --> 显示下一个界面 --> 点击“交易计算器”
    • 这个想法是创建能够处理菜单更改的测试
    • 尝试使用: string mouseOver(string $locator) 这模拟用户将鼠标悬停在指定元素上。 docs.tadiavo.com/phpunit/www.phpunit.de/pocket_guide/3.1/en/…
    【解决方案2】:

    使用所需选项值的 xpath 检查元素是否可见。

    $this->isElementPresent($xpath); $this->click($xpath);

    如果为真,则 click() 方法选择指定的选项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-12
      • 2016-11-12
      • 2018-03-19
      • 2014-10-06
      相关资源
      最近更新 更多