簡體   English   中英

如何使用Selenium WebDriver在嵌套的div中查找元素

[英]How to find elements in nested divs with Selenium WebDriver

我正在嘗試在Google Play商店中找到應用,但無法找到嵌套在其他元素中的元素。 我已經看過Java中的一些示例,但不確定我是否正確實現了Ruby。 我想選擇一個外部div容器,然后在其中選擇div,直到獲得應用程序標題並打印出來。

我已經閱讀了Selenium WebDriver的文檔,並嘗試閱讀了一些rubygem文檔,並且還進行了Google搜索以使用WebDriver查找嵌套的div元素。 這是我的第一個Selenium WebDriver項目。

i = 0

app_cards = driver.find_elements(:class_name, "card-content")
app_details = app_cards.find_elements(:class_name, "details")
app_titles = app_details.find_elements(:class_name, "title")

app_titles.each do |app_title|
    puts "App #{i}: " + app_title.attribute("title")
    i = i + 1
end

我希望腳本可以將所有應用程序結果打印在Google Play商店的頁面上,

main.rb:17:in `<main>': undefined method `find_elements' for #<Array:0x00005594417b5f68> (NoMethodError)

您可以執行以下操作以打印所有標題

require 'selenium-webdriver'
driver=Selenium::WebDriver.for :firefox
driver.manage.timeouts.implicit_wait = 20
driver.navigate.to('https://play.google.com/store/apps')
driver.find_elements(css: '.details > .title').to_enum.with_index(1) do |element,index|
  puts "App #{index}: " +element.attribute('title')
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM