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
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
I want to change the color of the upper control navigation bar (Control menu) in Liferay 7 / DXP
I want to output the day of the week
[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 know the answer of the rock-paper-scissors app
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
[Ruby] I want to make a program that displays today's day of the week!
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
I want to set the conditions to be displayed in collection_check_boxes
[Rails] [bootstrap] I want to change the font size responsively
Determine that the value is a multiple of 〇 in Ruby
I want to perform high-speed prime factorization in Ruby (ABC177E)
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
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
When you want to change the MySQL password of docker-compose
I want to transition to the same screen in the saved state
I want to narrow down the display of docker ps
I want to temporarily disable the swipe gesture of UIPageViewController
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 want to recreate the contents of assets from scratch in the environment built with capistrano
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
[Ruby] I want to put an array in a variable. I want to convert to an array
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 display the images under assets/images in the production environment
I want to remove the top margin in Grouped UITableView (swift)
[Java] I want to perform distinct with the key in the object
[Android] I want to get the listener from the button in ListView
How to increment the value of Map in one line in Java
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 display the number of orders for today using datetime.
Procedure to make the value of the property file visible in Spring Boot
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 thought about the best way to create a ValueObject in Ruby
I want to display an error message when registering in the database
[Ruby] I tried to summarize the methods that frequently appear in paiza