硒中的单击迭代失败 [英] click iteration fails in selenium

查看:37
本文介绍了硒中的单击迭代失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试翻译来自 tripadvisor 的用户评论.所以scraper读取链接,然后逐条遍历每条评论并翻译它们.但是我的代码在翻译第一条评论后就停止了.

Im trying to translate user comments from tripadvisor. So the scraper reads the link, then one by one iterates through each of the comments and translates them. But my code stops after translating the first comment itself.

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.tripadvisor.in/ShowUserReviews-g1-d8729164-r425811350-TAP_Portugal-World.html")
gt= driver.find_elements(By.CSS_SELECTOR,".googleTranslation>.link")
for i in gt:
    i.click()
    time.sleep(2)
    driver.find_element_by_class_name("ui_close_x").click()
    time.sleep(2)

推荐答案

试试这个:

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()
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)
    driver.find_element_by_class_name("ui_close_x").click()
    time.sleep(2)

这篇关于硒中的单击迭代失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆