需要 select 特定元素在使用 Java 的 Selenium Webdriver 中使用 XPath
Need to select specific element using XPath in Selenium Webdriver using Java
我遇到了一个找不到答案的问题。我通常擅长 selecting 这个意大利面条数字代码中的特定元素。这次我难住了。这是代码...
<div id="widget_dijit_form_DateTextBox_0_dropdown" class="dijitPopup dijitCalendarPopup" style="visibility: visible; top: 179.55px; z-index: 1000; left: 217.9px; right: auto; display: none;" role="presentation" dijitpopupparent="dijit_form_DateTextBox_0">
<table id="dijit_form_DateTextBox_0_popup" class="dijitCalendarContainer dijitCalendar" lang="" cellspacing="0" cellpadding="0" aria-labelledby="dijit_form_DateTextBox_0_popup_year" dojoattachevent="onkeypress: _onKeyPress" role="grid" style="-moz-user-select: none; top: 0px; overflow: hidden; width: 259px; visibility: visible;" dir="" widgetid="dijit_form_DateTextBox_0_popup">
<thead>
<tbody class="dijitReset dijitCalendarBodyContainer" dojoattachevent="onclick: _onDayClick, onmouseover: _onDayMouseOver, onmouseout: _onDayMouseOut, onmousedown: _onDayMouseDown, onmouseup: _onDayMouseUp">
<tr class="dijitReset dijitCalendarWeekTemplate" role="row">
<tr class="dijitReset dijitCalendarWeekTemplate" role="row">
<tr class="dijitReset dijitCalendarWeekTemplate" role="row">
<tr class="dijitReset dijitCalendarWeekTemplate" role="row">
<td class="dijitCalendarCurrentMonth dijitCalendarDateTemplate" role="gridcell" dijitdatevalue="1463904000000">
<td class="dijitCalendarCurrentMonth dijitCalendarDateTemplate" role="gridcell" dijitdatevalue="1463990400000">
<td class="dijitCalendarCurrentMonth dijitCalendarDateTemplate" role="gridcell" dijitdatevalue="1464076800000">
<td class="dijitCalendarCurrentMonth dijitCalendarDateTemplate" role="gridcell" dijitdatevalue="1464163200000">
<td class="dijitCalendarSelectedDate dijitCalendarCurrentMonth dijitCalendarDateTemplate" role="gridcell" dijitdatevalue="1464249600000" tabindex="0">
<td class="dijitCalendarCurrentMonth dijitCalendarDateTemplate" role="gridcell" dijitdatevalue="1464336000000">
<span class="dijitCalendarDateLabel">27</span>
</td>
...
...
<div id="widget_dijit_form_DateTextBox_1_dropdown" class="dijitPopup dijitCalendarPopup" style="visibility: visible; top: 225px; z-index: 1000; left: 217.9px; right: auto;" role="presentation" dijitpopupparent="dijit_form_DateTextBox_1">
<table id="dijit_form_DateTextBox_1_popup" class="dijitCalendarContainer dijitCalendar dijitCalendarHover dijitHover dijitCalendarFocused dijitCalendarHoverFocused dijitHoverFocused dijitFocused" lang="" cellspacing="0" cellpadding="0" aria-labelledby="dijit_form_DateTextBox_1_popup_year" dojoattachevent="onkeypress: _onKeyPress" role="grid" style="-moz-user-select: none; top: 0px; overflow: hidden; width: 255px; visibility: visible;" dir="" widgetid="dijit_form_DateTextBox_1_popup">
<thead>
<tbody class="dijitReset dijitCalendarBodyContainer" dojoattachevent="onclick: _onDayClick, onmouseover: _onDayMouseOver, onmouseout: _onDayMouseOut, onmousedown: _onDayMouseDown, onmouseup: _onDayMouseUp">
<tr class="dijitReset dijitCalendarWeekTemplate" role="row">
<tr class="dijitReset dijitCalendarWeekTemplate" role="row">
<tr class="dijitReset dijitCalendarWeekTemplate" role="row">
<tr class="dijitReset dijitCalendarWeekTemplate" role="row">
<tr class="dijitReset dijitCalendarWeekTemplate" role="row">
<td class="dijitCalendarCurrentMonth dijitCalendarDateTemplate" role="gridcell" dijitdatevalue="1432454400000">
<td class="dijitCalendarCurrentMonth dijitCalendarDateTemplate" role="gridcell" dijitdatevalue="1432540800000">
<td class="dijitCalendarCurrentDate dijitCalendarCurrentMonth dijitCalendarDateTemplate" role="gridcell" dijitdatevalue="1432627200000" tabindex="0">
<td class="dijitCalendarCurrentMonth dijitCalendarDateTemplate dijitCalendarHoveredDate" role="gridcell" dijitdatevalue="1432713600000">
<span class="dijitCalendarDateLabel">27</span>
</td>
我正在尝试 select 第二个“27”。如果我使用这个 xpath 语句
//*[contains(@class, 'dijitCalendarCurrentMonth')][contains(., '27')]
它return是两个元素。所以,我只需要 select
下面的那个
id="dijit_form_DateTextBox_1_popup"
我试过以下...全部return没有匹配的节点...
//*[@id='dijit_form_DateTextBox_1_popup' and contains(@class, 'dijitCalendarCurrentMonth') and contains(., '27')]
//*[@id='dijit_form_DateTextBox_1_popup'][contains(@class, 'dijitCalendarCurrentMonth')][contains(., '27')]
//*[@id='dijit_form_DateTextBox_1_popup'][contains(@class, 'dijitCalendarCurrentMonth') and contains(., '27')]
我做错了什么?
对于 [@id='dijit_form_DateTextBox_1_popup']
,您正在寻找 table
标签本身中的文本 27,它不包含文本 27
。如果您删除它,它应该找到包含 span
的 td
,其中包含文本 27。而且,由于 td
没有任何具有 [@id='dijit_form_DateTextBox_1_popup']
的 id
属性,它也找不到任何匹配项。
尝试
//table[@id='dijit_form_DateTextBox_1_popup']//span[contains(.,'27')]
我还看到 span
包含文本 27 是 table 中唯一的 span
。为了获得更好的性能,我认为您应该尝试以下 css。
#dijit_form_DateTextBox_1_popup span
我遇到了一个找不到答案的问题。我通常擅长 selecting 这个意大利面条数字代码中的特定元素。这次我难住了。这是代码...
<div id="widget_dijit_form_DateTextBox_0_dropdown" class="dijitPopup dijitCalendarPopup" style="visibility: visible; top: 179.55px; z-index: 1000; left: 217.9px; right: auto; display: none;" role="presentation" dijitpopupparent="dijit_form_DateTextBox_0">
<table id="dijit_form_DateTextBox_0_popup" class="dijitCalendarContainer dijitCalendar" lang="" cellspacing="0" cellpadding="0" aria-labelledby="dijit_form_DateTextBox_0_popup_year" dojoattachevent="onkeypress: _onKeyPress" role="grid" style="-moz-user-select: none; top: 0px; overflow: hidden; width: 259px; visibility: visible;" dir="" widgetid="dijit_form_DateTextBox_0_popup">
<thead>
<tbody class="dijitReset dijitCalendarBodyContainer" dojoattachevent="onclick: _onDayClick, onmouseover: _onDayMouseOver, onmouseout: _onDayMouseOut, onmousedown: _onDayMouseDown, onmouseup: _onDayMouseUp">
<tr class="dijitReset dijitCalendarWeekTemplate" role="row">
<tr class="dijitReset dijitCalendarWeekTemplate" role="row">
<tr class="dijitReset dijitCalendarWeekTemplate" role="row">
<tr class="dijitReset dijitCalendarWeekTemplate" role="row">
<td class="dijitCalendarCurrentMonth dijitCalendarDateTemplate" role="gridcell" dijitdatevalue="1463904000000">
<td class="dijitCalendarCurrentMonth dijitCalendarDateTemplate" role="gridcell" dijitdatevalue="1463990400000">
<td class="dijitCalendarCurrentMonth dijitCalendarDateTemplate" role="gridcell" dijitdatevalue="1464076800000">
<td class="dijitCalendarCurrentMonth dijitCalendarDateTemplate" role="gridcell" dijitdatevalue="1464163200000">
<td class="dijitCalendarSelectedDate dijitCalendarCurrentMonth dijitCalendarDateTemplate" role="gridcell" dijitdatevalue="1464249600000" tabindex="0">
<td class="dijitCalendarCurrentMonth dijitCalendarDateTemplate" role="gridcell" dijitdatevalue="1464336000000">
<span class="dijitCalendarDateLabel">27</span>
</td>
...
...
<div id="widget_dijit_form_DateTextBox_1_dropdown" class="dijitPopup dijitCalendarPopup" style="visibility: visible; top: 225px; z-index: 1000; left: 217.9px; right: auto;" role="presentation" dijitpopupparent="dijit_form_DateTextBox_1">
<table id="dijit_form_DateTextBox_1_popup" class="dijitCalendarContainer dijitCalendar dijitCalendarHover dijitHover dijitCalendarFocused dijitCalendarHoverFocused dijitHoverFocused dijitFocused" lang="" cellspacing="0" cellpadding="0" aria-labelledby="dijit_form_DateTextBox_1_popup_year" dojoattachevent="onkeypress: _onKeyPress" role="grid" style="-moz-user-select: none; top: 0px; overflow: hidden; width: 255px; visibility: visible;" dir="" widgetid="dijit_form_DateTextBox_1_popup">
<thead>
<tbody class="dijitReset dijitCalendarBodyContainer" dojoattachevent="onclick: _onDayClick, onmouseover: _onDayMouseOver, onmouseout: _onDayMouseOut, onmousedown: _onDayMouseDown, onmouseup: _onDayMouseUp">
<tr class="dijitReset dijitCalendarWeekTemplate" role="row">
<tr class="dijitReset dijitCalendarWeekTemplate" role="row">
<tr class="dijitReset dijitCalendarWeekTemplate" role="row">
<tr class="dijitReset dijitCalendarWeekTemplate" role="row">
<tr class="dijitReset dijitCalendarWeekTemplate" role="row">
<td class="dijitCalendarCurrentMonth dijitCalendarDateTemplate" role="gridcell" dijitdatevalue="1432454400000">
<td class="dijitCalendarCurrentMonth dijitCalendarDateTemplate" role="gridcell" dijitdatevalue="1432540800000">
<td class="dijitCalendarCurrentDate dijitCalendarCurrentMonth dijitCalendarDateTemplate" role="gridcell" dijitdatevalue="1432627200000" tabindex="0">
<td class="dijitCalendarCurrentMonth dijitCalendarDateTemplate dijitCalendarHoveredDate" role="gridcell" dijitdatevalue="1432713600000">
<span class="dijitCalendarDateLabel">27</span>
</td>
我正在尝试 select 第二个“27”。如果我使用这个 xpath 语句
//*[contains(@class, 'dijitCalendarCurrentMonth')][contains(., '27')]
它return是两个元素。所以,我只需要 select
下面的那个id="dijit_form_DateTextBox_1_popup"
我试过以下...全部return没有匹配的节点...
//*[@id='dijit_form_DateTextBox_1_popup' and contains(@class, 'dijitCalendarCurrentMonth') and contains(., '27')]
//*[@id='dijit_form_DateTextBox_1_popup'][contains(@class, 'dijitCalendarCurrentMonth')][contains(., '27')]
//*[@id='dijit_form_DateTextBox_1_popup'][contains(@class, 'dijitCalendarCurrentMonth') and contains(., '27')]
我做错了什么?
对于 [@id='dijit_form_DateTextBox_1_popup']
,您正在寻找 table
标签本身中的文本 27,它不包含文本 27
。如果您删除它,它应该找到包含 span
的 td
,其中包含文本 27。而且,由于 td
没有任何具有 [@id='dijit_form_DateTextBox_1_popup']
的 id
属性,它也找不到任何匹配项。
尝试
//table[@id='dijit_form_DateTextBox_1_popup']//span[contains(.,'27')]
我还看到 span
包含文本 27 是 table 中唯一的 span
。为了获得更好的性能,我认为您应该尝试以下 css。
#dijit_form_DateTextBox_1_popup span