I want to change the value of Attribute in Selenium of Ruby

Overview

When writing scraping process in Ruby's Selenium, there is a scene where you really want to change the attribute value. It is not used when you want to faithfully track the behavior of the UI, but it is useful when you know the behavior of the UI and want to skip the troublesome process and get the desired one immediately.

Method

Access to Attribute in Ruby does not find SetAttribute even though it has GetAttribute. (It may be a problem of how to find it) Get does not change the DOM, but I guess that it is not prepared because it may cause unexpected behavior in writing, so this time I will use "execute_script" To use.

execute_script.rb


#Driver initialization
client = Selenium::WebDriver::Remote::Http::Default.new
driver = Selenium::WebDriver.for :chrome, http_client: client

#<div id="mw-content-text" lang="ja" dir="ltr" class="mw-content-ltr">
#I want to change ja to en

#Get the target element
target_element = driver.find_element(:class_name, 'mw-content-ltr')
driver.execute_script("arguments[0].setAttribute('lang', 'en');", target_element)

reference

https://www.rubydoc.info/gems/selenium-webdriver/0.0.28/Selenium%2FWebDriver%2FDriver:execute_script

Recommended Posts

I want to change the value of Attribute in Selenium of Ruby
I want to get the value in Ruby
[Ruby] I want to extract only the value of the hash and only the key
I want to change the log output settings of UtilLoggingJdbcLogger
[Ruby] I want to reverse the order of the hash table
I want to change the color of the upper control navigation bar (Control menu) in Liferay 7 / DXP
I want to var_dump the contents of the intent
[Ruby] I want to output only the odd-numbered characters in the character string
I tried to make a parent class of a value object in Ruby
[Ruby] I want to display posted items in order of newest date
How to change the value of a variable at a breakpoint in intelliJ
I tried to make full use of the CPU core in Ruby
I want to embed any TraceId in the log
I want to get the value of Cell transparently regardless of CellType (Apache POI)
I want to display the name of the poster of the comment
I want to return the scroll position of UITableView!
I want to change the path after new registration after logging in with multiple devises.
[Active Admin] I want to specify the scope of the collection to be displayed in select_box
[Rails] I want to display the link destination of link_to in a separate tab
[Rails] [bootstrap] I want to change the font size responsively
I want to perform high-speed prime factorization in Ruby (ABC177E)
How to retrieve the hash value in an array in Ruby
How to change the setting value of Springboot Hikari CP
I tried to summarize the basic grammar of Ruby briefly
I want to give a class name to the select attribute
I want to create a Parquet file even in Ruby
I want to simplify the conditional if-else statement in Java
I want to control the display of the upper management navigation bar (Control menu) in Liferay 7 / DXP
I tried to solve the problem of "multi-stage selection" with Ruby
I want to understand the flow of Spring processing request parameters
The story of Collectors.groupingBy that I want to keep for posterity
Want to know what Ruby n is the power of 2? (Power judgment of 2)
I want to limit the input by narrowing the range of numbers
I want to remove the top margin in Grouped UITableView (swift)
[Java] I want to perform distinct with the key in the object
I want to control the default error message of Spring Boot
[Android] I want to get the listener from the button in ListView
How to increment the value of Map in one line in Java
[Controller] I want to retrieve the numerical value of a specific column from the DB (my memo)
I want to use @Autowired in Servlet
I want to know the Method of the Controller where the Exception was thrown in the ExceptionHandler of Spring Boot
[Rails] I want to send data of different models in a form
I want to write JSP in Emacs more easily than the default.
I want to know the JSP of the open portlet when developing Liferay
How to change the maximum and maximum number of POST data in Spark
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
I want to pass the argument of Annotation and the argument of the calling method to aspect
I want to get the IP address when connecting to Wi-Fi in Java
I want to display an error message when registering in the database
[Ruby] I tried to summarize the methods that frequently appear in paiza ②
Android development, how to check null in the value of JSON object
I want you to use Enum # name () for the Key of SharedPreference
Change the save destination of the image to S3 in the Rails app. Part 2
Summary of how to select elements in Selenium
How to find the cause of the Ruby error
I want to sort by tab delimited by ruby
I want to send an email in Java.
I tried to organize the session in Rails
The story of introducing Ajax communication to ruby
[Ruby] I want to do a method jump!
I want to pass APP_HOME to logback in Gradle