我想单击“目标线”的“正在播放”页面下的所有矩形图标(图标看起来像足球场)。我尝试了很多次,但是没有用。如果有人知道如何单击所有图标,请帮助我。
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()
答案 0 :(得分:0)
(移动评论以回答问题)
两个问题:
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