【问题标题】:How can I perform an action based on the contents of a div with Selenium Webdriver?如何使用 Selenium Webdriver 根据 div 的内容执行操作?
【发布时间】:2016-06-16 06:47:30
【问题描述】:

我有一个使用 Selenium Webdriver 和 Nokogiri 的 Ruby 应用程序。我想选择一个类,然后对于该类对应的每个div,我想根据div的内容执行一个动作。

例如,我正在解析以下页面:

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

这是一个搜索结果页面,我正在寻找描述中包含“收养”一词的第一个结果。因此,机器人应该寻找带有className: "result" 的 div,检查其.description div 是否包含“adoption”一词,如果包含,请单击.link div。换句话说,如果.description 不包含该词,那么机器人将继续前进到下一个.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

【问题讨论】:

    标签: ruby selenium selenium-webdriver webdriver bots


    【解决方案1】:

    您可以通过 XPath 使用 contains() 获取包含“adopt”和“Adopt”的元素列表,然后使用联合运算符 (|) 将“adopt”和“Adopt”的结果合并。见以下代码:

    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
    

    【讨论】:

      【解决方案2】:

      处理每次迭代的模式将取决于对每个项目执行的操作类型。如果该操作是单击,则您无法列出所有要单击的链接,因为第一次单击将加载新页面,从而使元素列表过时。 因此,如果您希望单击每个链接,那么一种方法是使用包含每次迭代的链接位置的 XPath:

      # 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
      

      这是一个点击结果页面中每个链接的示例:

      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!"
      

      【讨论】:

        【解决方案3】:

        我不会用 ruby​​ 编写代码,但你可以用 python 编写代码的一种方法是:

        driver.find_elements
        

        注意元素是复数的,我会抓取所有链接并将它们放入一个数组中。

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

        然后以相同的方式获取所有描述。如果描述中包含“采用”一词,则对描述的每个元素执行一个 for 循环,然后导航到该网站。

        例如:

        如果描述[6] 有“收养”一词,则查找字符串 href[6] 并导航到 href[6]。

        我希望这是有道理的!

        【讨论】:

          猜你喜欢
          • 2016-12-15
          • 2013-12-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多