MacchiattoMark / blog

learning notes
0 stars 0 forks source link

Selenium + ChromeDriver #5

Open MacchiattoMark opened 5 years ago

MacchiattoMark commented 5 years ago

配置文档:

brew install chromedriver brew tap homebrew/cask brew cask install chromedriver

手动安装:

步骤二: 修改文件权限

sudo chmod u+x,o+x /usr/local/bin/chromedriver

检查是否安装成功:

chromedriver --version

# python:
* https://seleniumhq.github.io/selenium/docs/api/py/
* https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver
* 等待页面完全加载(js loading): https://selenium-python-zh.readthedocs.io/en/latest/waits.html

pip install selenium

* 示例代码:

```python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()

browser.get('http://www.yahoo.com')
assert 'Yahoo' in browser.title

elem = browser.find_element_by_name('p')  # Find the search box
elem.send_keys('seleniumhq' + Keys.RETURN)

browser.quit()
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

driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()