问题描述
作为Selenium练习(仍在学习如何使用它),我一直试图使其以无头模式打开chrome,请访问以下网站:https://adobe-flash-player.en.softonic.com/download,然后单击该下载按钮(“免费下载Windows版”)。
这是我的代码:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os
options= Options()
options.headless=True
options.add_argument("--window-size=1920x1080")
chrome_driver = os.getcwd() +"\\chromedriver.exe"
print(os.getcwd())
driver = webdriver.Chrome(options=options,executable_path=chrome_driver)
driver.get("https://adobe-flash-player.en.softonic.com/download")
lucky_button = driver.find_elements_by_class_name("btn btn--medium btn--free-download mb-s track-btn-download")
print(lucky_button)
lucky_button.click()
出现错误:
Traceback (most recent call last):
File "C:/Users/aniru/AppData/Local/Programs/Python/Python38-32/falsdwn.py",line 15,in <module>
lucky_button.click()
AttributeError: 'list' object has no attribute 'click'
似乎正在将该按钮读取为列表,但是如果是这样,我无法理解为什么。我将如何解决这个问题,为什么会这样?
解决方法
要点击下载按钮,请使用以下任一CSS选择器。
代码:
lucky_button = driver.find_element_by_css_selector(".btn.btn--medium.btn--free-download.mb-s.track-btn-download")
lucky_button.click()
OR
lucky_button=driver.find_element_by_css_selector("a[data-auto='download-button']")
lucky_button.click()
更新:
诱导WebDriverWait
()并等待visibility_of_element_located
()并诱使javascript执行程序单击该按钮。
lucky_button=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.CSS_SELECTOR,".btn.btn--medium.btn--free-download.mb-s.track-btn-download")))
driver.execute_script("arguments[0].click();",lucky_button)
您需要导入以下库。
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
更新:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("headless")
options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(chrome_options=options)
#provide your download path
driver.execute_cdp_cmd('Page.setDownloadBehavior',{'behavior': 'allow','downloadPath': r'D:\kk\Downloads'})
driver.get("https://adobe-flash-player.en.softonic.com/download")
download_button=WebDriverWait(driver,download_button)
print("Done")
您可以在此处找到讨论的链接Downloading file in headless mode
,要以无头模式打开Chrome,我使用:
options = webdriver.ChromeOptions()
options.add_argument("headless")
第二个问题(您的例外情况):
lucky_button = driver.find_elements_by_class_name("btn btn--medium btn--free-download mb-s track-btn-download")
if len(lucky_button): # found at least one
lucky_button[0].click() # use first one
更新
我不确定为什么您的find_elements_by_class_name
无法正常工作。但是,这似乎确实找到了可点击的内容(不幸的是,但并非始终如此):
lucky_buttons = driver.find_elements_by_partial_link_text('Free Download')
所以代替:
lucky_buttons[0].click()
使用:
driver.execute_script("arguments[0].click();",lucky_buttons[0])
我使用的完整代码:
from selenium import webdriver
options= webdriver.ChromeOptions()
#options.headless=True
options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(options=options)
try:
driver.implicitly_wait(5)
driver.get("https://adobe-flash-player.en.softonic.com/download")
lucky_buttons = driver.find_elements_by_partial_link_text('Free Download')
print(lucky_buttons)
if len(lucky_buttons):
driver.execute_script("arguments[0].click();",lucky_buttons[0])
finally:
input('pausing ...')
driver.quit()
更新2
from selenium import webdriver
options= webdriver.ChromeOptions()
options.headless=True
options.add_experimental_option('excludeSwitches',['enable-logging'])
options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(options=options)
try:
driver.implicitly_wait(5)
driver.get("https://adobe-flash-player.en.softonic.com/download")
lucky_buttons = driver.find_elements_by_partial_link_text('Free Download')
print(lucky_buttons)
if len(lucky_buttons):
driver.execute_script("arguments[0].click();",lucky_buttons[0])
elements = driver.find_elements_by_class_name('message-download__title')
print('elements =',elements)
finally:
driver.quit()
打印:
[<selenium.webdriver.remote.webelement.WebElement (session="d1ed56ed6e0f43a470f0a24f1a4ba798",element="8e88c2a7-633d-4df9-b0de-e5e27ddc21b3")>]
elements = [<selenium.webdriver.remote.webelement.WebElement (session="d1ed56ed6e0f43a470f0a24f1a4ba798",element="66acebf6-4971-42c5-9395-1c7913b5effd")>]