[英]iterate through classes in selenium python
** 我在彈出頁面上運行它。 因此不能簡單地使用“條目”類,因為它與原始頁面發生沖突。
我想遍歷類以從“入門”類中挑選出文本
from selenium import webdriver
driver=webdriver.Firefox()
當我從 chrome 中選擇這個元素的 xpath 時,它是這樣的
但這driver.find_element_by_xpath(/html/body/span/div[1]/div/div[3])
用於driver.find_element_by_xpath(/html/body/span/div[1]/div/div[3])
但下面的一個工作,但它給了我日期,標題等。但我只想要文本
driver.find_element_by_class_name("ui_overlay").text
考慮使用這個:
for div in driver.find_elements_by_class_name("entry"):
do_something(div.text)
編輯后的問題:這必須使用xpath解決。
for div in driver.find_elements_by_xpath("//span/div/div/div[@class='entry']"):
do_something(div)
如果您提供您正在使用的網頁,我可以使用更精確的xpath。
試試這個xpath它會正確定位:
".//span[@class = 'ui_overlay ui_modal ']//div[@class='entry']"
使用示例:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome("C:\Jars\chromedriver.exe")
driver.maximize_window()
url="https://www.tripadvisor.com/Airline_Review-d8729164-Reviews-Cheap-Flights-TAP-Portugal#REVIEWS"
driver.get(url)
wait = WebDriverWait(driver, 10)
langselction = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.sprite-date_picker-triangle")))
langselction.click()
driver.find_element_by_xpath("//div[@class='languageList']//li[normalize-space(.)='Portuguese first']").click()
gt= driver.find_elements(By.CSS_SELECTOR,".googleTranslation>.link")
for i in gt:
i.click()
time.sleep(2)
x = driver.find_element(By.XPATH, ".//span[@class = 'ui_overlay ui_modal ']//div[@class='entry']")
print x.text
driver.find_element_by_class_name("ui_close_x").click()
time.sleep(2)
它會打印相應的文字
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.