Selenium单击一个按钮

时间:2017-05-22 03:37:26

标签: selenium

我想点击一个按钮:

<button type="button" class="yt-ui-menu-item yt-uix-menu-close-on-select vm-playlist-monetize-videos">
    <span class="yt-ui-menu-item-label">Monetize</span>
</button>

我尝试了以下内容:

//*[@id=\"aria-menu-id-7\"]/ul/li[1]/button/span

哪个不起作用,因为id有时不同于7

我也已经完成了

//span[.='Monetize']

哪个也行不通。如何获取xpath以单击上面的按钮?

3 个答案:

答案 0 :(得分:0)

如果 id 属性不唯一,您可以尝试使用使用功能启动。请尝试按照 xpath

进行操作
//*[starts-with(@id, 'aria-menu-id')]/descendant::button/span[text()='Monetize']

如果它对您有用,请告诉我。

答案 1 :(得分:0)

使用explicit wait方法和xpath尝试以下代码。

WebDriverWait wait = new WebDriverWait(driver, 10);  //wait for 10 seconds to find the web-elment.
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//button/span[text()='Monetize']"))));
WebElement button = driver.findElement(By.xpath("//button/span[text()='Monetize']"));

答案 2 :(得分:0)

单击按钮而不是span元素

//*[text()='Monetize'][@class='yt-ui-menu-item-label']/..
(or use the below format)
//*[contains(text(),'Monetize')][@class='yt-ui-menu-item-label']/..