简体   繁体   English

从Facebook的硒和PHP WebDriver的下拉菜单中选择选项

[英]Select option from dropdown with selenium and php webdriver from facebook

I'm trying to put together a script with PHP facebook/webdriver 1.4.1 and selenium 3.5 that will automate the task of giving online orders to our selected transport company (TNT) because they do not provide any rest api to fulfill this function and it is really tedious do it every singel time by hand. 我正在尝试将脚本与PHP facebook / webdriver 1.4.1和selenium 3.5放在一起,该脚本将自动化向我们选择的运输公司(TNT)发出在线订单的任务,因为它们不提供任何其他api来实现此功能,并且每次手动操作都非常乏味。

My script works fine except for the Selectboxes, that are jscript generated with a bunch of <ul> and <li> , and I can not select the desired values. 我的脚本工作正常,但“选择框”除外,这些选择框是用一堆<ul><li>生成的jscript,我无法选择所需的值。

This is an example of the select 这是选择的一个例子

 <td> <select id="latestCollectionTime" name="latestCollectionTime" style="display: none;"> <option value="" selected="selected"> selecteer... </option> <option value="2230"> 22:30 </option> <option value="2245"> 22:45 </option> <option value="2300"> 23:00 </option> <option value="2315"> 23:15 </option> <option value="2330"> 23:30 </option> <option value="2345"> 23:45 </option> </select> <span id="latestCollectionTime-dropdown" class="selectboxit-container"> <span id="latestCollectionTimeSelectBoxIt" class="selectboxit dropdown-menu" style="" name="latestCollectionTime" tabindex="0" unselectable="on" role="combobox" aria-autocomplete="list" aria-expanded="false" aria-owns="latestCollectionTimeSelectBoxItOptions" aria-activedescendant="0" aria-label="" aria-live="assertive"> <i id="latestCollectionTimeSelectBoxItDefaultIcon" class="selectboxit-default-icon selectboxit-option-icon" unselectable="on" style="margin-top: 5.5px;"> </i> <span id="latestCollectionTimeSelectBoxItText" class="selectboxit-text" unselectable="on" data-val="" style="line-height: 22px; max-width: 88px;"> selecteer... </span> <span id="latestCollectionTimeSelectBoxItArrowContainer" class="selectboxit-arrow-container" unselectable="on" style="height: 22px;"> <i id="latestCollectionTimeSelectBoxItArrow" class="selectboxit-arrow caret" unselectable="on" style="margin-top: 7px;"> </i> </span> </span> <ul id="latestCollectionTimeSelectBoxItOptions" class="selectboxit-options" tabindex="-1" role="listbox" aria-hidden="true" style="max-height: 198px; top: auto; display: none;"> <li id="0" data-val="" data-disabled="false" class="selectboxit-option selectboxit-option-first" style="" role="option"> <a class="selectboxit-option-anchor"><i class="selectboxit-option-icon " style="margin-top: 3.5px;"></i>selecteer...</a> </li> <li id="1" data-val="2230" data-disabled="false" class="selectboxit-option" style="" role="option"> <a class="selectboxit-option-anchor"><i class="selectboxit-option-icon " style="margin-top: 3.5px;"></i>22:30</a> </li> <li id="2" data-val="2245" data-disabled="false" class="selectboxit-option" style="" role="option"> <a class="selectboxit-option-anchor"><i class="selectboxit-option-icon " style="margin-top: 3.5px;"></i>22:45</a> </li> <li id="3" data-val="2300" data-disabled="false" class="selectboxit-option" style="" role="option"> <a class="selectboxit-option-anchor"><i class="selectboxit-option-icon " style="margin-top: 3.5px;"></i>23:00</a> </li> <li id="4" data-val="2315" data-disabled="false" class="selectboxit-option" style="" role="option"> <a class="selectboxit-option-anchor"><i class="selectboxit-option-icon " style="margin-top: 3.5px;"></i>23:15</a> </li> <li id="5" data-val="2330" data-disabled="false" class="selectboxit-option" style="" role="option"> <a class="selectboxit-option-anchor"><i class="selectboxit-option-icon " style="margin-top: 3.5px;"></i>23:30</a> </li> <li id="6" data-val="2345" data-disabled="false" class="selectboxit-option selectboxit-option-last" style="" role="option"> <a class="selectboxit-option-anchor"><i class="selectboxit-option-icon " style="margin-top: 3.5px;"></i>23:45</a> </li> </ul> </span> </td> 
The <li> id is automatic generated, and duplicated in other selectboxes in the page, so i can't $driver->findElement(WebDriverBy::id('6'))->click(); <li> id是自动生成的,并且在页面的其他选择框中重复,因此我无法$driver->findElement(WebDriverBy::id('6'))->click(); .

I can't select the normal way. 我无法选择正常方式。

 $driver->findElement( WebDriverBy::id('latestCollectionTime') ) ->findElement( WebDriverBy::cssSelector("option[value='11']") ) ->click(); 

because the select is hidden style="display: none; and neither with the xpath. 因为选择是隐藏的style="display: none;且xpath都没有。

I would like to be able to directly select a value like data-val="2330" but taking in consideration that are other 3 selectboxes with the same <li> ids and data-val values, but with diferent <span> and <ul> ids . 我希望能够直接选择一个值,例如data-val="2330"但要考虑到其他3个具有相同<li> id和data-val值,但具有不同<span><ul> ID。

Can anyone help me with this? 谁能帮我这个? Thanks in advance. 提前致谢。

EDIT: 编辑:

I finish using it like this: 我像这样完成使用它:

 //make the dropdow list visible. $driver->findElement(WebDriverBy::id('latestCollectionTimeSelectBoxIt'))->click(); // to click in the desired option. $driver->findElement(WebDriverBy::xPath('//*[@id = "latestCollectionTimeSelectBoxItOptions"]/li[@data-val = "'.$desired_value.'"]'))->click(); 

Thanks again to @DAN in the answer below for pointing me in the right direction with the xPath sintax. 再次感谢@DAN在下面的答案中使用xPath sintax指向正确的方向。

Firstly, elements with duplicate ids are not valid HTML, and it is up to each browser to determine what they will do with the illegal page elements. 首先,具有重复ID的元素不是有效的HTML,并且由每个浏览器决定如何处理非法页面元素。 As far as I know, most browsers will allow multiple DOM elements with duplicate IDs, but this may change in the future. 据我所知,大多数浏览器将允许多个具有重复ID的DOM元素,但是将来可能会改变。

Secondly, you can select the element you're interested in via XPath. 其次,您可以通过XPath选择您感兴趣的元素。 For example, the XPath (//*[@id = 'someID'])[1] selects the first element with an id of someID . 例如,XPath (//*[@id = 'someID'])[1]选择ID为someID的第一个元素。

However, as you mentioned that the <ul> elements have different IDs, you can use XPath such as //ul[@id = 'latestCollectionTimeSelectBoxItOptions']/li[@data-val = '2230'] to select the element. 但是,正如您提到的<ul>元素具有不同的ID一样,您可以使用XPath(例如//ul[@id = 'latestCollectionTimeSelectBoxItOptions']/li[@data-val = '2230']来选择元素。

Finally, you mention that the list items are not clickable because they are hidden, presumably because they're part of a dropdown menu. 最后,您提到列表项不可单击,因为它们是隐藏的,大概是因为它们是下拉菜单的一部分。

In this case, you need to firstly click on the dropdown to open it, then find and click on the appropriate list item. 在这种情况下,您需要首先单击下拉列表以将其打开,然后查找并单击相应的列表项。

Hope this helps. 希望这可以帮助。

You only have to click on the option you want to choose. 您只需单击要选择的选项。

I am using xpath for that because i want to access the option from the given select box. 我之所以使用xpath,是因为我想从给定的选择框中访问该选项。

$optionFromSelectBox = $driver->findElement(
    WebDriverBy::xpath(
      '//select[@id=\'latestCollectionTimeSe‌​lectBoxIt\']//option[2]'
    )
);

$optionFromSelectBox->click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM