【问题标题】:I am having problems selecting button in selenium for java我在为 java 选择 selenium 中的按钮时遇到问题
【发布时间】:2021-12-15 04:57:03
【问题描述】:

您好,我正在尝试选择并单击“立即预订”按钮,但是当我查看源代码时,它显示以下内容...

<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>

[为什么会有两个按钮 class="btn btn-secondary text-uppercase btn-landing go-to-session" ??]

我尝试用

选择第一个实例
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

任何帮助将不胜感激!

【问题讨论】:

    标签: java selenium selenium-chromedriver junit5


    【解决方案1】:

    定位器不需要使用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

    【讨论】:

      【解决方案2】:

      请尝试以下 xpath :

      //div[contains(text(),'Book now')]
      

      PS:如果我们在HTML DOM 中有唯一条目,请检查dev tools(谷歌浏览器)。

      检查步骤:

      Press F12 in Chrome -> 转到element 部分 -> 做一个CTRL + F -> 然后粘贴xpath 看看,如果你想要的element 用@ 得到高亮 987654329@匹配节点。

      如果有多个条目,请尝试索引:

      (//div[contains(text(),'Book now')])[1]
      

      (//div[contains(text(),'Book now')])[2]
      

      事实上,你可以有[3][4] 和任何数字,但你必须确保它在 HTML DOM 中是 1/1 匹配的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-21
        • 2014-12-08
        • 1970-01-01
        • 2022-11-16
        • 1970-01-01
        • 2019-02-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多