在selenium python中迭代类

在selenium python中迭代类,python,selenium,web-scraping,Python,Selenium,Web Scraping,**我在弹出页面上运行这个。因此,不能简单地使用“entry”类,因为它与原始页面冲突 我想遍历类,从“entry”类中挑选文本 当我从chrome中选取这个元素的xpath时,它是这样出现的 但这不适用于驱动程序。通过xpath(/html/body/span/div[1]/div/div[3])查找元素 但下面的一个是工作,但它给我的日期,标题等,但我只想要文本 driver.find_element_by_class_name("ui_overlay").text 尝试此xpath,

**我在弹出页面上运行这个。因此,不能简单地使用“entry”类,因为它与原始页面冲突

我想遍历类,从“entry”类中挑选文本

当我从chrome中选取这个元素的xpath时,它是这样出现的

但这不适用于
驱动程序。通过xpath(/html/body/span/div[1]/div/div[3])查找元素

但下面的一个是工作,但它给我的日期,标题等,但我只想要文本

driver.find_element_by_class_name("ui_overlay").text 

尝试此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)

它将打印相应的文本

尝试此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)

它将打印相应的文本

如何获取文本。在使用这个@thebadguy rocks!之后,我得到了这个输出!。但是你能告诉我你是如何得到xpath的吗?在我的chrome中,当我右键单击并复制xpath时,它只是/html/body/span/div[1]/div/div[3],基本上它是在创建自定义xpath。而不是从chrome复制xpath。请也接受它作为答案。@thebadguy代码不稳定地失败了我如何才能得到文本。在使用这个@thebadguy rocks!之后,我得到了这个输出!。但是你能告诉我你是如何得到xpath的吗?在我的chrome中,当我右键单击并复制xpath时,它只是/html/body/span/div[1]/div/div[3],基本上它是在创建自定义xpath。而不是从chrome复制xpath。请也接受它作为答案。@thebadguy代码不规则地失败了