Skip to main content
 首页 » 编程设计

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

2025年05月04日101sxdcgaq8080

我要单击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()