[JAVA] What to do if you can't get the text of an element in Selenium

What to do if you can't get the text of an element well with Selenium, which is often used for scraping.

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.

If the class name contains spaces

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.

Next, if the class name matches but the text cannot be obtained

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 thespan 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 thediv` 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

image.png

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!

Recommended Posts

What to do if you can't get the text of an element in Selenium
[Rails] What to do if you can't get an error message with the errors method
What to do if you get a java.io.IOException in GlassFish
What to do if you can't use the rails command
What to do if you get a wrong number of arguments error in binding.pry
What to do if you get the error Couldn't find Item without an ID
What to do if you get a gcc error in Docker
What to do if you get a DISPLAY error in gym.render ()
What to do if you forget the root password in CentOS7
[Rails] What to do if you can't get parameters with form_with
What to do if you get an error in Basic authentication during Rails test code
What to do if you get an Argument Error: wrong number of arguments (given 2, expected 0) in your RSpec test
What to do if you get a groovy warning in Thymeleaf Layout
What to do if you get an error during rails db: reset
What to do if you get an uninitialized constant Likes Controller error
What to do if you can't install the plugin from the Eclipse marketplace
Get the type of an array element to determine if it is an array
What to do if you get an error when you hit Heroku logs
What to do if you get an [An HTTP request took too long to complete.] Error in Docker.
What to do if the Rails server can't start
What to do if you can't activate the select box created by bootstrap-select
What to do if you get the error message unrecognized selector send to instance "***"
What to do if you get an error on heroku rake db: migrate
What to do if you get To install the missing version, run `gem install bundler: 2.1.4`
What to do if you get an error with bundle install by entering gem'bcrypt' in your Gemfile
What to do if you can't bundle update and bundle install after installing Ruby 3.0.0 in the Rails tutorial
What to do if you get an "A server is already running." Error when you try to start the rails server
What to do if you get a javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake in the IBM JDK
[Java] What to do if you get an error in Eclipse saying "Not allowed at source level below 1.X"
What to do if you get the warning "Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1." in Rails 6.0
What to do if you can't find the JDK you put in SDKMAN after migrating from bash to zsh on macOS
What to do if you don't see the test code error message in the terminal console
[Rails] What to do if you accidentally install bundle in the production environment in your local environment
What to do if you get a "302" error in your controller unit test code in Rails
What to do if the Rails page doesn't appear in Rails tutorial 1.3.2
What to do if Cloud9 is full in the Rails tutorial
What to do if you get angry with OpenSSL with pyenv install
How to get the length of an audio file in java
What to do if you cannot roll back the migration (UnknownMigrationVersionError)
What to do if you install Ubuntu
What to do if you select a JRE in Eclipse and get "The selected JRE does not support the current compliance level 11"
What to do if you get a "Mysql2 :: Error: Operand should contain 1 column (s)" error in Rails
[Note] What to do if bundle install in Chapter 3 of the rails tutorial is not possible
What to do if you get a JNI shared library error when trying to build in Eclipse
What to do if the prefix c is not bound in JSP
What to do if you don't like the code generated by swagger-codegen-cli
What to do if you get a MiniMagick vulnerability alert on GitHub
What to do if you get further occurrences of http header parsing errors will be logged at debug level. In tomcat 8.5.37
What to do if the server tomcat dies
What to do if you push incorrect information
What to do if Operation not permitted is displayed when you execute a command in the terminal
What to do if you get the error Too long with no output (exceeded 10m0s) on CircleCI
[Swift] How to get the number of elements in an array (super basic)
What to do if you get a SQLite3 :: BusyException: database is locked error
What to do when you think you can't do Groovy-> Java in IntelliJ IDEA CE
If you want to satisfy the test coverage of private methods in JUnit
What to do if you get Could not locate Gemfile or .bundle / directory
What to do if the changes are not reflected in the jar manifest file
What to do if you get angry if you don't have nokogiri while installing wp2txt
What to do if ffi installation fails when launching an application in Rails
What to do if you have enabled Use the WSL2 based engine in Docker Desktop with insufficient WSL2 installation