简体   繁体   English

如何通过 Selenium 中的文本(Java)找到带有 Selenium 的按钮?

[英]How to find button with Selenium by its text inside (Java)?

I have a button like below.我有一个像下面这样的按钮。 I am not sure how to grab it by it's text which is Search .我不知道如何通过它的文本来抓取它Search There are many buttons on this page.这个页面上有很多按钮。

<button _ngcontent-dqg-c23="" class="cp-mat-small-btn mat-flat-button mat-accent" color="accent" mat-flat-button="" ng-reflect-disabled="false" ng-reflect-color="accent"><span class="mat-button-wrapper">
    Search
  </span><div class="mat-button-ripple mat-ripple" matripple="" ng-reflect-centered="false" ng-reflect-disabled="false" ng-reflect-trigger="[object HTMLButtonElement]"></div><div class="mat-button-focus-overlay"></div></button>

I tried :我试过 :

WebElement searchBtn = webDriver.find_element_by_xpath('//button[text()="Search"]');

But getting error:但得到错误:

    | > Task :compileTestJava
gauge_1          | /tipu/src/test/java/CalmImplementation.java:16: error: unclosed character literal
gauge_1          |         WebElement searchBtn = webDriver.find_element_by_xpath('//button[normalize-space()="Search"]');
gauge_1          |                                                                ^
gauge_1          | /tipu/src/test/java/CalmImplementation.java.java:16: error: unclosed character literal
gauge_1          |         WebElement searchBtn = webDriver.find_element_by_xpath('//button[normalize-space()="Search"]');
gauge_1          |                                                                                                     ^
gauge_1          | /tipu/src/test/java/CalmImplementation.java.java:16: error: not a statement
gauge_1          |         WebElement searchBtn = webDriver.find_element_by_xpath('//button[normalize-space()="Search"]');
gauge_1          |                                                                         ^
gauge_1          | 3 errors
gauge_1          | 

You can use XPATH:您可以使用 XPATH:

//button[normalize-space()="Search"]

Note that search by text()="Search" won't work as button text is not exactly "Search" - text content contains linesbreaks/spaces请注意,按text()="Search"将不起作用,因为按钮文本不完全是"Search" - 文本内容包含换行符/空格

find_element_by_xpath() isn't a valid method when using Selenium 's Java client. find_element_by_xpath()在使用SeleniumJava客户端时不是有效的方法。

Again, the WebElement with text as Search is within the child <span> of it's parent <button> and an Angular element.同样,文本为SearchWebElement位于其父<button>Angular元素的子<span>中。

So to identify the element by it's text ie Search you you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies :因此,要通过文本(即搜索)识别元素,您需要为elementToBeClickable()引入WebDriverWait ,您可以使用以下任一定位器策略

  • Using xpath and contains() :使用xpathcontains()

     WebElement searchBtn = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button/span[contains(., 'Search')]")));
  • Using xpath and normalize-space() :使用xpathnormalize-space()

     WebElement searchBtn = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button/span[normalize-space()='Search']")));

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

相关问题 如何在网页上查找文本并使用Selenium和Java获取其XPath或CSS - How to find text on a webpage and get its XPath or CSS with Selenium and Java 如何使用 Selenium 和 Java 提取不在其自身标签内的文本节点的文本? - How to extract the text of a text node that is not inside a tag of its own using Selenium and Java? 如何根据其文本断言元素-使用JAVA的Selenium WebDriver - How to assert an element based on its Text - Selenium WebDriver with JAVA 如何使用Java和Selenium在其文本的基础上获取元素索引? - How to get element index on the basis of its text using Java and Selenium? 如何在Java中使用硒单击Excel工作表中的按钮 - How to click on button inside Excel Sheet using selenium in java 如何使用硒中的属性和文本查找html标签? - How to find the html tag using its attribute and its text both in selenium? 如何使用Selenium WebDriver Java从标签内部获取文本? - How to get text from inside tag use selenium webdriver java? 如何使用Java在Selenium中通过文本查找Web元素? - How to find a Web Element by text in Selenium using Java? 如何在 Java 中使用 selenium web 驱动程序查找突出显示的文本? - How to find the highlighted text using selenium web driver in Java? 印地语文本验证哪个在按钮内 + selenium - Hindi text verify which is inside button + selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM