美文网首页
配置python+selenium+mac(FireFox)

配置python+selenium+mac(FireFox)

作者: elf_fff | 来源:发表于2019-12-08 19:36 被阅读0次

1. install selenium

pip install selenium

2. download latest 'geckodriver'

2.1 the method below is no use as web issue

brew install geckodriver
export PATH=$PATH:/path/to/geckodriver
source ~/.bash_profile

2.2 download it from github and config manually

website

https://github.com/mozilla/geckodriver/releases/tag/v0.26.0

go to installed folder

cd /usr/local/bin

unpack it

tar xzf geckodriver-v0.26.0-macos.tar.gz

make it executable

chmod +x geckodriver

instantiate Firefox browser instance providing path to the geckodriver

from selenium import webdriver
driver = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
driver.get('http://example.com')

3. An example code

from selenium import webdriver

# Create a new Firefox session
driver = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
driver.implicitly_wait(30)
driver.maximize_window()

# Navigate to the application home page
driver.get("https://www.baidu.com")

# Get the search textbox
search_field = driver.find_element_by_name("wd")
search_field.clear()

# Enter search keyword and submit
search_field.send_keys("phones")
search_field.submit()

# Get all the anchor elements which have products names displayed
# currently on the result page using find_elements_by_xpath method
products = driver.find_elements_by_xpath("//h2[@class='product-name']/a")

# Get the number of anchor elements found
print("Found " + str(len(products)) + " products:")

# Iterate through each anchor element and print the text that
# is name of the product
for product in products:
    print(product.text)

# Close the browser window
driver.quit()

4. Selenium Doc

official doc: https://www.seleniumhq.org/docs/
Python API: https://selenium.dev/selenium/docs/api/py/api.html
Chinese doc: https://selenium-python-zh.readthedocs.io/en/latest/

相关文章

网友评论

      本文标题:配置python+selenium+mac(FireFox)

      本文链接:https://www.haomeiwen.com/subject/xnzmgctx.html