We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
开发环境
macOS 10.12.6
Python 3.5.5 | packaged by conda-forge
Selenium:conda install selenium 参考:https://selenium-python.readthedocs.io/installation.html 这里使用 conda update selenium更新至selenium3.14.1
conda install selenium
conda update selenium
ChromeDriver 77.0.3865.40(Chrome版本77.0.3865.90 。注意:下载的驱动 ChromeDriver 的版本要对应 Chrome 的版本) Selenium 操作 Chrome 浏览器需要下载 ChromeDriver 驱动来协助:ChromeDriver - WebDriver for Chrome: https://chromedriver.chromium.org/ --> new ChromeDriver site: https://sites.google.com/chromium.org/driver/ ChromeDriver 环境变量配置:https://python3webspider.cuiqingcai.com/1.2-qing-qiu-ku-de-an-zhuang#5-huan-jing-bian-liang-pei-zhi 。可以不配置,而是在代码中通过 driver = webdriver.Chrome('<Path to ChromeDriver>') 指定 ChromeDriver 的位置。
driver = webdriver.Chrome('<Path to ChromeDriver>')
直接调起 Chrome 浏览器执行自动化操作(不是操作无头的Chrome)。
from selenium import webdriver from selenium.common.exceptions import TimeoutException import time # 调用环境变量指定的ChromeDriver创建浏览器对象。这里因为没有在环境变量配置指定ChromeDriver,需指定ChromeDriver位置。 # driver = webdriver.Chrome(executable_path='/Users/fatli/python/chromedriver') # 绝对路径 driver = webdriver.Chrome(executable_path='./chromedriver') # 相对路径 try: driver.get('<login_url>') except TimeoutException: print('访问登录页面超时') username_field = driver.find_element_by_id('username') password_field = driver.find_element_by_id('password') # submit_button = driver.find_element_by_tag_name('button') submit_button = driver.find_element_by_css_selector('button.ng-binding') # 清空输入框(二次登录时该页面会记住用户名,如果不清除,则会在记住的用户名后追加用户名) username_field.clear() username_field.send_keys('<username>') password_field.send_keys('<password>') submit_button.click() time.sleep(5) # driver.close() # Closes the current window. driver.quit() # Quits the driver and closes every associated window.
关于无头Chrome:https://developers.google.com/web/updates/2017/04/headless-chrome
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 from selenium.common.exceptions import TimeoutException, NoSuchElementException import time # 操作无头chrome浏览器 # from selenium.webdriver.chrome.options import Options # chrome_options = Options() options = webdriver.ChromeOptions() options.add_argument('--headless') # 启动无头模式 # 调用环境变量指定的ChromeDriver创建浏览器对象。这里因为没有在环境变量配置指定ChromeDriver,需指定chromedriver位置。 driver = webdriver.Chrome(options=options, executable_path='./chromedriver') try: driver.get('<login_url>') # 截屏(登录前) driver.get_screenshot_as_file('./01before_login.png') except TimeoutException: print('访问登录页面超时') try: username_field = driver.find_element_by_id('username') password_field = driver.find_element_by_id('password') # submit_button = driver.find_element_by_tag_name('button') submit_button = driver.find_element_by_css_selector('button.ng-binding') except NoSuchElementException: print('No Such Element: 登录表单的某些元素') # 清空输入框(二次登录时该页面会记住用户名,如果不清除,则会在记住的用户名后追加用户名) username_field.clear() username_field.send_keys('<username>') password_field.send_keys('<password>') # 截屏(登录中) driver.get_screenshot_as_file('./02logging_in.png') # 点击提交登录按钮失败:“Element is not interactable”,参考: # stackoverflow.com/questions/52859981/selenium-generating-error-element-is-not-interactable submit_button.click() try: # WebDriverWait和expected_conditions两个模块组合起来构成Selenium的显式等待 # github.com/FatliTalk/blog/issues/125 element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'visit-list-talbe')) ) except Exception as e: print('抛出异常:') print(e) finally: # 截屏(登录后) driver.get_screenshot_as_file('./03after_login.png') print(driver.find_element_by_tag_name('tbody').text) # print(driver.find_element(By.TAG_NAME, 'tbody').text) # 作用同上 # driver.close() # Closes the current window. driver.quit() # Quits the driver and closes every associated window.
The text was updated successfully, but these errors were encountered:
No branches or pull requests
开发环境
macOS 10.12.6
Python 3.5.5 | packaged by conda-forge
Selenium:
conda install selenium
参考:https://selenium-python.readthedocs.io/installation.html这里使用
conda update selenium
更新至selenium3.14.1ChromeDriver 77.0.3865.40(Chrome版本77.0.3865.90 。注意:下载的驱动 ChromeDriver 的版本要对应 Chrome 的版本)
Selenium 操作 Chrome 浏览器需要下载 ChromeDriver 驱动来协助:ChromeDriver - WebDriver for Chrome: https://chromedriver.chromium.org/ --> new ChromeDriver site: https://sites.google.com/chromium.org/driver/
ChromeDriver 环境变量配置:https://python3webspider.cuiqingcai.com/1.2-qing-qiu-ku-de-an-zhuang#5-huan-jing-bian-liang-pei-zhi 。可以不配置,而是在代码中通过
driver = webdriver.Chrome('<Path to ChromeDriver>')
指定 ChromeDriver 的位置。一、Selenium直接操作Chrome浏览器
二、Selenium操作headless-chrome
The text was updated successfully, but these errors were encountered: