gpt4 book ai didi

python - 使用 selenium 单击元素时出现问题

转载 作者:行者123 更新时间:2023-12-01 03:39:12 24 4
gpt4 key购买 nike

我使用此代码来探索 tripadvisor(葡萄牙语评论)

from selenium import webdriver
from bs4 import BeautifulSoup
driver=webdriver.Firefox()
driver.get("https://www.tripadvisor.com/Airline_Review-d8729164-Reviews-Cheap-Flights-TAP-Portugal#review_425811350")
driver.set_window_size(1920, 1080)

然后我尝试点击谷歌翻译链接

driver.find_element_by_class_name("googleTranslation").click()

但出现此错误:-

WebDriverException: Message: Element is not clickable at point (854.5, 10.100006103515625). Other element would receive the click: <div class="inner easyClear"></div>

因此 div class="inner easyClear" 获得了点击。我尝试探索它

from bs4 import BeautifulSoup
page=driver.page_source
for i in page.findAll("div","easyClear"):
print i
print "================="

但无法从中获得任何直觉,比如现在需要进行哪些更改才能使“谷歌翻译”可点击。请帮忙

==================================编辑=============== ===============

我也尝试过这些

driver.execute_script("window.scrollTo(0, 1200);")
driver.find_element_by_class_name("googleTranslation").click()

将浏览器大小调整为全屏等..

最佳答案

对我有用的是使用显式等待和 element_to_be_clickable 预期条件并获取内部 span 元素:

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://www.tripadvisor.com.br/ShowUserReviews-g1-d8729164-r425802060-TAP_Portugal-World.html")

wait = WebDriverWait(driver, 10)

google_translate = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".googleTranslation .link")))

actions = ActionChains(driver)
actions.move_to_element(google_translate).click().perform()

您可能还会进入“调查”或“促销”弹出窗口 - 请务必考虑到这些内容。

关于python - 使用 selenium 单击元素时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39939995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com