image - Selenium webdriver:我想单击https://www.msn.com/zh-cn/weather/today底部的“Like 1.3M”图像

标签 image selenium xpath iframe webdriver

我要单击https://www.msn.com/en-in/weather/today底部的“像1.3M”图像

我尝试了多种方法来单击“ facebook like”图像,例如切换到iframe,然后在webdriver中单击图像。

但是他们都没有工作。

最佳答案

由于您没有指定任何语言,因此我将使用Python回答您的问题。
首先,您需要切换到包含以下类似按钮的iframe

driver.get("https://www.msn.com/en-in/weather/today")
iframe = WebDriverWait(driver, 30).until(expected_conditions.presence_of_element_located((By.XPATH, '//*[@id="fbcount"]/iframe')))
iframe.location_once_scrolled_into_view
driver.switch_to.frame(iframe)


然后点击:

driver.find_element_by_xpath('//*[@id="u_0_0"]/div/button').click()


然后切换到默认内容

driver.switch_to.default_content()



如果得到NoSuchElementException,请添加一些等待时间。

这是完整的代码

import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome("/chromedriver/location")

driver.get("https://www.msn.com/en-in/weather/today")
iframe = WebDriverWait(driver, 30).until(expected_conditions.presence_of_element_located((By.XPATH, '//*[@id="fbcount"]/iframe')))
iframe.location_once_scrolled_into_view
driver.switch_to.frame(iframe)
time.sleep(3)
driver.find_element_by_xpath('//*[@id="u_0_0"]/div/button').click()
time.sleep(3)
driver.switch_to.default_content()

driver.close()
driver.quit()


关于image - Selenium webdriver:我想单击https://www.msn.com/zh-cn/weather/today底部的“Like 1.3M”图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59335840/

相关文章:

html - 如何让内容(主要是图像)根据 tumblr 上的窗口大小缩放或调整大小?

C# - 在运行时更新 pictureBox.Image 导致错误

image - 自定义 UITableViewCell 中的 UIImageView 不尊重 Aspect Fit 模式

php - 在 PHP 或 Unix 命令行中确定图像分辨率和文件类型的最快方法?

python - 在selenium中使用 "webdriver.Chrome()"时出错

javascript - 浏览器已完成加载, capybara 仍无法通过测试

c++ - 使用 libxml++ 为 XPath 注册命名空间

testing - 如何使用 XPATH 访问 TR 内的链接

python - 如何在 python 中使用 Selenium 关闭浏览器弹出窗口?

python - 如何在源代码(Xpath)中查找特定字符串并提取后续文本?