迭代selenium python中的类

时间:2016-10-12 08:57:40

标签: python selenium web-scraping

**我在弹出页面上运行它。所以不能简单地使用“入门”类,因为它与原始页面冲突。

我想通过类来迭代从“条目”类

中挑选文本
from selenium import webdriver
driver=webdriver.Firefox()

enter image description here

当我从chrome中选择这个元素的xpath时,它就像这样来了

但这不适用于driver.find_element_by_xpath(/html/body/span/div[1]/div/div[3])

但是下面有一个正在工作,但是它给了我约会,标题等等。但我只想要文本

driver.find_element_by_class_name("ui_overlay").text 

2 个答案:

答案 0 :(得分:1)

试试这个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)

它会打印相应的文字

答案 1 :(得分:0)

考虑使用:

[ExcelFunction(Description = "A useful test function that adds two numbers, and returns the sum.")]
public static double AddThem(
    [ExcelArgument(Name = "Augend", Description = "is the first number, to which will be added")] 
    double v1,
    [ExcelArgument(Name = "Addend", Description = "is the second number that will be added")]     
    double v2)
{
    return v1 + v2;
}

编辑后的问题: 然后必须使用xpath解决这个问题。

for div in driver.find_elements_by_class_name("entry"):
    do_something(div.text)

如果您提供您正在使用的网页,我可以使用更精确的xpath。