我想从页面中单击"进球线"下的所有矩形图标(图标看起来像足球场(。我试了很多次,但都不起作用。如果有人知道如何点击这些图标,请帮助我。
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_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()
(移动注释以回答(
两个问题:
- 您正在使用
find_element_by_css_selector
搜索类和标记。将此更改为仅搜索类名 - 要返回元素集合,请使用返回元素列表的
find_elements_by_css_selector
替换此代码:
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