您好,我正在嘗試選擇并單擊“立即預訂”按鈕,但當我查看源代碼時,它顯示以下內容...
<div class="pl-0 mr-3 sticky-btn-wrapper">
<div class="ko-container book-now-btn-container">
<button class="btn btn-secondary text-uppercase btn-landing go-to-session" data-eid="757231" data-aid="97739" data-isavailable="true"> Book now</button>
</div>
</div>
<div class="btn-book-top sticky-btn-wrapper justify-content-end" id="book-button-top">
<div id="sticky-bottom-btn" class="sticky-bottom-btn flex-row w-100">
<div class="ko-container book-now-btn-container">
<button class="btn btn-secondary text-uppercase btn-landing go-to-session" data-eid="757231" data-aid="97739" data-isavailable="true">Book now</button></div>
</div>
</div>
</div>
當我檢查 Firefox 中的“立即預訂”鏈接時顯示以下內容
<div class="ko-container book-now-btn-container">
<button class="btn btn-secondary text-uppercase btn-landing go-to-session" data-eid="757231" data-aid="97739" data-isavailable="true">
Book now
</button>
</div>
[為什么有兩個 button 實體??]
我試圖選擇第一個實體
WebElement wb = myDriver.findElement(By.xpath ("//div[@class='pl-0 mr-3 sticky-btn-wrapper'] and button[@class='btn btn-secondary text-uppercase btn-landing go-to-session']"));
wb.click();
但是我在 Junit 中遇到以下例外...
org.openqa.selenium.InvalidSelectorException:
Given xpath expression "//div[@class='pl-0 mr-3 sticky-btn-wrapper'] and button[@class='btn btn-secondary text-uppercase btn-landing go-to-session']"
is invalid: TypeError: Document.evaluate: Result type mismatch
任何幫助將不勝感激!
uj5u.com熱心網友回復:
您不需要and為定位器使用運算元,只需將 xpath 更改為:
//div[@class='pl-0 mr-3 sticky-btn-wrapper']//button
and當您有兩個或更多相似元素并且需要添加更多限制時,您需要使用關鍵字。另外and的說法應適用于相同的標簽,而不是不同。
例如:
<div class='1' style='1'>
<button>Button1</button>
</div>
<div class='1' style='2'>
<button>Button2</button>
</div>
<div class='2' style='2'>
<button>Button3</button>
</div>
如果您需要將定位器定位到 Button2:
//div[@class='1' and @style='2']/button
uj5u.com熱心網友回復:
請嘗試以下 xpath :
//div[contains(text(),'Book now')]
PS:請檢查dev tools(谷歌瀏覽器)我們是否有唯一的條目HTML DOM。
檢查步驟:
Press F12 in Chrome- >去element節- >做一個CTRL F- >再貼上xpath看看,如果你需要的element是越來越強調與1/1匹配的節點。
如果有多個條目,請嘗試索引:
(//div[contains(text(),'Book now')])[1]
或者
(//div[contains(text(),'Book now')])[2]
實際上,您可以擁有[3],[4]和任何數字,但是您必須確保在 HTML DOM 中它是 1/1 匹配。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/346999.html
