简体   繁体   English

给定一个bySelector,我如何获得该元素的孩子的选择器?

[英]given a bySelector how can i get the selector of this element's child?

This is my Html : 这是我的HTML

<a class="info-button" data-voice-actions="[&quot;info&quot;, &quot;more info&quot;]" href="waze://?button=external_poi_info" id="info-button" onclick="window.location.href = this.href; return false;">
<span class="icon"></span>
<div class="btn-text">More Info</div>
</a>

I have a by.org.openqa.selenium.By selector: 我有一个by.org.openqa.selenium.By选择器:

By.cssSelector("#main > div > div.footer > div > a")

and I want sometimes to relate to itself, and once I want to fetch its text as in its <div class="btn-text">More Info</div> child element. 有时我想与自己相关,一旦我想获取其文本,就如其<div class="btn-text">More Info</div>子元素中一样。

How can I generically get the selector of the child element from the original selector I have? 如何从我拥有的原始选择器中一般性地获取子元素的选择器?

(I ask generically because this case repeats few times for me) (我一般都会问,因为这种情况对我重复了几次)

If I understood the problem you are facing clearly then two things you can do. 如果我清楚地理解了您面临的问题,那么您可以做两件事。

First, Write a crazy selector to find it. 首先,编写一个疯狂的选择器来找到它。

//a[@class='info-button']/..//div[.='More Info']

Second, Find the parent element and from there find the child element 其次,找到父元素,然后从那里找到子元素

driver.findElement(By.cssSelector("#main > div > div.footer > div > a")).findElement(By.path("//div[.='More Info']")).something

Ultimately, it depends on how your application works. 最终,这取决于应用程序的工作方式。 The following assumes that the element you are looking for is always in immediate child of your a element, and that it always has the .btn_text class. 下面假设你正在寻找的元素总是在你眼前的孩子a元素,它总是有.btn_text类。 You can just manipulate selectors like any string: 您可以像处理任何字符串一样操作选择器:

String parent_sel = "#main > div > div.footer > div > a";
String child_sel = parent_sel + " > div.btn_text";

Both strings can be passed to By.cssSelector . 这两个字符串都可以传递给By.cssSelector If my assumptions are incorrect, adapt as needed. 如果我的假设不正确,请根据需要进行调整。 For instance if the div can be a descendant of a without necessarily be a child of it, you could concatenate " div.btn_text" to get the child_sel . 举例来说,如果div可以的后代a ,而不一定是它的孩子,你可以串连" div.btn_text"获得child_sel

I want to fetch its text 我想获取其文字

In case you were looking for a selector that will give you text , I'll note that there is no selector you can pass to findElement to get text . 如果您正在寻找可以为您提供文本的选择器,我会注意到没有可以传递给findElement选择器来获取文本 This function always returns elements (or list of elements when used in its plural form). 此函数始终返回元素(或以其复数形式使用时的元素列表)。 You'll have to use getText() on the element you get. 您必须在获取的元素上使用getText() (And in case you wonder: XPath in general can select text, but not when you use it through Selenium.) (并且,如果您想知道:XPath通常可以选择文本,但是当您通过Selenium使用它时则不能。)

You can use the below xpath to locate the "info-button": 您可以使用下面的xpath找到“信息按钮”:

//div[@class='footer']//a[@class='info-button']

To retrieve the text "More Info", you can use the below xpath: 要检索文本“更多信息”,可以使用以下xpath:

//div[@class='footer']//div[.='More Info']

Edit as per OP's comment 根据OP的评论进行编辑

The css-selector to get to the div element with innerHTML/text can be this: 使用innerHTML / text到达div元素的css-selector可以是这样的:

#main > div > div.footer > div > a > div:nth-child(1)

It locates the first child element 'div' under the concerned 'a' element. 它将第一个子元素“ div”定位在相关的“ a”元素下。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM