我想从页面中单击“进行中的球门线”下的所有矩形图标(图标看起来像足球场)。我尝试了很多次但没有成功。如果有人知道如何单击这些所有图标,请帮助我。from time import sleepfrom selenium import webdriverfrom selenium.webdriver.chrome.options import Optionschrome_options = Options()# chrome_options.add_argument("--headless")chrome_options.add_argument('--start-maximized')driver = webdriver.Chrome(options=chrome_options, executable_path=r"chromedriver.exe")driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {"source": """Object.defineProperty(navigator, 'webdriver', {get: () => undefined})"""})url = 'https://www.bet365.com/#/IP/B1'driver.get(url)sleep(10)res = driver.execute_script("return document.documentElement.outerHTML")sleep(10)click_icon = driver.find_element_by_css_selector(".ovm-MediaIconContainer_Buttons div")click_icon.click()
1 回答
aluckdog
TA贡献1847条经验 获得超7个赞
(移动评论来回答)
两个问题:
您正在使用 搜索类和标签
find_element_by_css_selector
。将此更改为仅搜索类名。要返回元素集合,请使用
find_elements_by_css_selector
it 返回元素列表。
替换这段代码:
click_icon = driver.find_element_by_css_selector(".ovm-MediaIconContainer_Buttons div") click_icon.click()
有了这个:
icon_list = driver.find_elements_by_css_selector(".ovm-MediaIconContainer_Buttons") for icon in icon_list: # every icon in list icon.click() # click single icon
添加回答
举报
0/150
提交
取消