简体   繁体   English

使用XPath字符串的Selenium select下拉元素

[英]Selenium select dropdown element using xpath string

I have quite specific problem regarding both selenium and xpath. 关于硒和xpath,我有一个非常具体的问题。

I have to make an automated tests based on scenarios using selenium. 我必须根据使用硒的场景进行自动化测试。 All pages are automatically generated and using Ids is impossible. 所有页面都是自动生成的,因此无法使用ID。

All form elements though are designed in same way. 尽管所有表单元素都以相同的方式设计。

 <table>
 <tr><td> Title </td></tr>
 <tr><td> input/dropdown/etc </td></tr>
 </table>

He is the specifics 他是细节

<tr>
<td width="34%" valign="top" bgcolor="#ffffc7">
<span class="bold">Status wniosku</span>
<span>Test</span>
</td>
<td width="66%" bgcolor="#ffffc7">
<select id="ctl00_ContentPlaceHolder_2041" class="baseCtrl" name="ctl00$ContentPlaceHolder$2041">
<option value="" selected="selected">- wybierz -</option>
<option value="save">tylko zapisz</option>
<option value="pj">zapisz i wyślij do PJ</option>
</select>
<span>
</span>
<span id="ctl00_ContentPlaceHolder_ctl19" class="validation" style="display:none;">Określ status wniosku</span>
<span id="ctl00_ContentPlaceHolder_ctl20" class="validation" style="display:none;"></span>
<span></span>
</td>
</tr>

Using http://www.xmlme.com/XpathTool.aspx I have designed xpath for dropdown elements. 使用http://www.xmlme.com/XpathTool.aspx,我为下拉列表元素设计了xpath。

//span[text()='LABELNAME']/ancestor::*[1]/following-sibling::*/select/option[text()='TEXTVALUE']

I would like to use Selenium to click on the element i found. 我想使用Selenium单击找到的元素。 I've tried Selenium.Click() and variations of Selenium.Select() but with no results. 我已经尝试过Selenium.Click()和Selenium.Select()的变体,但是没有结果。

My question is, is the xpath designed correctly? 我的问题是,xpath设计正确吗? If so how should i execute it using Selenium? 如果是这样,我应该如何使用Selenium执行它? Thx for the help. 谢谢。

Try below xpath for selecting second option 尝试在xpath下选择第二个选项

"//span[text()='Test']/ancestor::*[1]/following-sibling::*/selec‌​t"

Ex: 例如:

Selenium.Select("//span[text()='Test']/ancestor::*[1]/following-sibling::*/selec‌​t","label=Save");

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

相关问题 Xpath Select 元件 Selenium - Xpath Select Element Selenium 选择在文档加载后加载的html元素,使用xpath的硒查找元素来选择该元素 - Selecting an html element that is loaded after documents loads, using selenium find element by xpath to select the element Selenium IDE使用xpath和index获取元素 - Selenium IDE get element using xpath and index 使用Selenium,xpath找不到&#39;text&#39;元素 - Using Selenium, xpath cannot find 'text' element 如何使用硒遍历非选择下拉? - How to Traverse Non-Select Dropdown using selenium? 硒无法在XPath中找到SVG元素 - Selenium cannot find SVG element in XPath How can I select item from dropdown, when option's element not interactable via selenium/python? - How can I select item from dropdown, when option's element not interactable via selenium/python? 在Selenium中,如何在不使用XPath的情况下访问具有相同类名的第二个图像元素? - In selenium, how do I access the second image element with the same class name, without using XPath? 使用复选框下拉,需要选择并取消选中具有select函数的复选框,并使用selenium获取相关值 - Dropdown with checkbox, need to select and unselect checkbox with select function and get the associated value using selenium 硒:选择第一个内部元素 - Selenium: select first inner element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM