I'm trying to test a complicated javascript interface with Selenium (using the Python interface, and across multiple browsers). I have a number of buttons of the form:
我正在尝试使用Selenium测试一个复杂的javascript接口(使用Python接口,并跨多个浏览器)。我有一些按钮的表格:
<div>My Button</div>
I'd like to be able to search for buttons based on "My Button" (or non-case-sensitive, partial matches such as "my button" or "button")
我希望能够基于“我的按钮”(或非大小写敏感的部分匹配,如“我的按钮”或“按钮”)搜索按钮
I'm finding this amazingly difficult, to the extent to which I feel like I'm missing something obvious. The best thing I have so far is:
我发现这非常困难,以至于我觉得我错过了一些显而易见的东西。到目前为止,我所拥有的最好的东西是:
driver.find_elements_by_xpath('//div[contains(text(), "' + text + '")]')
This is case-sensitive, however. The other thing I've tried is iterating through all the divs on the page, and checking the element.text property. However, every time you get a situation of the form:
然而,这是区分大小写的。我还尝试了遍历页面上的所有div,并检查元素。文本属性。然而,每当你得到一种形式的情况:
<div class="outer"><div class="inner">My Button</div></div>
div.outer also has "My Button" as the text. To fix THAT, I've tried looking to see if div.outer is the parent of div.inner, but couldn't figure out how to do that (element.get_element_by_xpath('..') returns an element's parent, but it tests not equal to div.outer). Also, iterating through all the elements on the page seems to be really slow, at least using the Chrome webdriver.
div.outer中还有“我的按钮”作为文本。为了解决这个问题,我尝试了查看div是否为div的父元素,但不知道如何实现它(element.get_element_by_xpath('..')返回一个元素的父元素,但它测试不等于div。此外,遍历页面上的所有元素似乎非常慢,至少使用Chrome webdriver是如此。
Ideas?
想法吗?
Edit: This question came out a little vague. Asked (and answered) a more specific version here: How to get text of an element in Selenium WebDriver (via the Python api) without including child element text?
这个问题有点含糊。问(并回答)一个更具体的版本:如何在不包含子元素文本的情况下获得Selenium WebDriver中的元素的文本(通过Python api) ?
5 个解决方案
#1
175
Try the following:
试试以下:
driver.find_elements_by_xpath("//*[contains(text(), 'My Button')]")
#2
20
you could try an xpath like:
您可以尝试使用xpath:
'//div[contains(text(), "{0}") and @class="inner"]'.format(text)
#3
4
You can also use it with Page Object Pattern, e.g:
你也可以用它来表示页面对象模式,例如:
Try this code:
试试这段代码:
@FindBy(xpath = "//*[contains(text(), 'Best Choice')]")
WebElement buttonBestChoice;
#4
2
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(), 'YourTextHere')]")));
assertNotNull(driver.findElement(By.xpath("//*[contains(text(), 'YourTextHere')]")));
String yourButtonName=driver.findElement(By.xpath("//*[contains(text(), 'YourTextHere')]")).getAttribute("innerText");
assertTrue(yourButtonName.equalsIgnoreCase("YourTextHere"));
#5
-12
Try this. Its very easy:
试试这个。它很简单:
driver.getPageSource().contains("text to search");
This really worked for me in selenium web driver.
这对我在selenium web驱动程序中很有用。
#1
175
Try the following:
试试以下:
driver.find_elements_by_xpath("//*[contains(text(), 'My Button')]")
#2
20
you could try an xpath like:
您可以尝试使用xpath:
'//div[contains(text(), "{0}") and @class="inner"]'.format(text)
#3
4
You can also use it with Page Object Pattern, e.g:
你也可以用它来表示页面对象模式,例如:
Try this code:
试试这段代码:
@FindBy(xpath = "//*[contains(text(), 'Best Choice')]")
WebElement buttonBestChoice;
#4
2
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(), 'YourTextHere')]")));
assertNotNull(driver.findElement(By.xpath("//*[contains(text(), 'YourTextHere')]")));
String yourButtonName=driver.findElement(By.xpath("//*[contains(text(), 'YourTextHere')]")).getAttribute("innerText");
assertTrue(yourButtonName.equalsIgnoreCase("YourTextHere"));
#5
-12
Try this. Its very easy:
试试这个。它很简单:
driver.getPageSource().contains("text to search");
This really worked for me in selenium web driver.
这对我在selenium web驱动程序中很有用。