How can I perform an action based on the contents of a div with Selenium Webdriver?

I have a Ruby application using Selenium Webdriver and Nokogiri. I want to select a class, and then for each div corresponding to this class, I want to perform an action based on the contents of the div.

For example, I am parsing the following page:

https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies 

This is a search results page, and I'm looking for the first result with the word "Adoption" in the description. Therefore, the bot should look for divs with className: "result" , because everyone checks to see if their .description div contains the word "acceptance", and if so, click on the .link div. In other words, if .description does not include this word, then the bot proceeds to the next .result .

This is what I still have that just clicks on the first result:

 require "selenium-webdriver" require "nokogiri" driver = Selenium::WebDriver.for :chrome driver.navigate.to "https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies" driver.find_element(:class, "link").click 
+5
source share
3 answers

You can get a list of elements that contain XPath "accept" and "Accept" using contains (), then use the union (|) operator to combine the results from "take" and "Adopt". See the following code:

 driver = Selenium::WebDriver.for :chrome driver.navigate.to "https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies" sleep 5 items = driver.find_elements(:xpath,"//div[@class='g']/div[contains(.,'Adopt')]/h3/a|//div[@class='g']/div[contains(.,'adopt')]/h3/a") for element in items linkText = element.text print linkText element.click end 
+6
source

The sample for processing each iteration will be determined by the type of action performed for each element. If the action is a click, you cannot list all the links to click on each of them, since the first click will load a new page, which will make the list of items obsolete. So, if you want to click on each link, then one way is to use XPath, which contains the link position for each iteration:

 # iteration 1 driver.find_element(:xpath, "(//h3[@class='r']/a)[1]").click # click first link # iteration 2 driver.find_element(:xpath, "(//h3[@class='r']/a)[2]").click # click second link 

Here is an example that clicks on each link on the results page:

 require 'selenium-webdriver' driver = Selenium::WebDriver.for :chrome wait = Selenium::WebDriver::Wait.new(timeout: 10000) driver.navigate.to "https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies" # define the xpath search_word = "Puppies" xpath = ("(//h3[@class='r']/a[contains(.,'%s')]" % search_word) + ")[%s]" # iterate each result by inserting the position in the XPath i = 0 while true do # wait for the results to be loaded wait.until {driver.find_elements(:xpath, "(//h3[@class='r']/a)[1]").any?} # get the next link link = driver.find_elements(:xpath, xpath % [i+=1]).first break if !link # click the link link.click # wait for a new page wait.until {driver.find_elements(:xpath, "(//h3[@class='r']/a)[1]").empty?} # handle the new page puts "Page #{i}: " + driver.title # return to the main page driver.navigate.back end puts "The end!" 
+2
source

I am not coding in ruby, but one way to do this in python is:

 driver.find_elements 

Note that the elements are multiple, I would capture all the links and put them in an array like.

 href = driver.find_elements_by_xpath("//div[@class='rc]/h3/a").getAttribute("href"); 

Then get all the descriptions the same way. Make a for loop for each item in the description, if the description contains the word "Adoption" in it, go to this website.

eg:

if in the description of [6] the word "acceptance" finds the string href [6] and go to href [6].

Hope this makes sense!

+1
source

Source: https://habr.com/ru/post/1244331/


All Articles