Easy to get hooked --The class name contains spaces --The text cannot be obtained even though the class name is correct.
This time, I will summarize how to deal with these two points.
With JavaScript, even if spaces are included
document.getElementByClassName("class name").innerText;
Just write the class name as it is, like You can easily get the text element of the specified class name. If you want to replace it with Selenium,
WebDriver.findElement(By.cssSelector("div[class='class name']")).getText().toString();
Using By.cssSelector like
If you specify with WebDriver.findElement (By.cssSelector ("{tag name} [class ='{class name}'] ")")
, you can get the text element well.
I haven't explicitly investigated the types of element tags that can be obtained with Selenium,
Mainly limited to div
, ʻa,
table,
tr,
td To identify the
span tag, the
btag and other elements, specify the element directly in Xpath or specify the element directly. You can get it by specifying the class name of the element of the
div` tag in the parent element of the element you want to get.
<div class="div_class_name">
<b class="b_class_name">
<span class="span_class_name">The text you want to get</span>
</b>
</div>
In this case, you can get the text by using the class name " div_class_name
"in the div
tag.
When using Xpath With the mouse focused on the element using Chrome's development tools Right click → [Copy] → [Copy XPath] Since you can get the XPath with
WebDriver.findElement(By.xpath("{xpath}")).getText();
You can get the text with.
that's all.
It deviates from the theme of the article, Since there is a slight discrepancy in scrolling between JavaScript and Selenium, You need to be careful about that as well.
Even if you can get it well with JavaScript, sometimes it does not work as expected with Selenium, so In the worst case, it is possible to embed JavaScript code in Java code and run it, so All you need to know is that!