!! python から firefox を操作する https://www.selenium.dev/downloads/ !chrome は Tips-selenium-chrome ! インストール pip3 install selenium wget https://github.com/mozilla/geckodriver/releases/download/v0.29.0/geckodriver-v0.29.0-linux64.tar.gz ### wget https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-linux64.tar.gz ! インストール pip3 install --user selenium tar xvfz geckodriver-v0.26.0-linux64.tar.gz cp ./geckodriver $HOME/bin/ *https://www.selenium.dev/selenium/docs/api/py/ *https://github.com/mozilla/geckodriver *https://github.com/mozilla/geckodriver/releases ! サンプル from selenium.webdriver import Firefox, FirefoxOptions from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium import webdriver # from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.select import Select from selenium.webdriver.common.alert import Alert from selenium.common.exceptions import TimeoutException options = FirefoxOptions() # ヘッドレスモードを有効にする(次の行をコメントアウトすると画面が表示される)。 options.add_argument('-headless') # FirefoxのWebDriverオブジェクトを作成する。 driver = Firefox(options=options) # Googleのトップ画面を開く。 driver.get('https://www.google.co.jp/') # タイトルに'Google'が含まれていることを確認する。 assert 'Google' in driver.title # スクリーンショットを撮る。 driver.save_screenshot('firefox_search_results.png') driver.quit() # ブラウザーを終了する。 ! サンプル PROXY from selenium.webdriver import Firefox, FirefoxOptions from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC ## from selenium import webdriver # from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.select import Select from selenium.webdriver.common.alert import Alert from selenium.common.exceptions import TimeoutException # ######################################### firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX firefox_capabilities['marionette'] = True PROXYs = "127.0.0.1:18080" firefox_capabilities['proxy'] = { "proxyType": "MANUAL", "httpProxy": PROXYs, "ftpProxy": PROXYs, "sslProxy": PROXYs } ######################################### ######################################### options = FirefoxOptions() # ヘッドレスモードを有効にする(次の行をコメントアウトすると画面が表示される)。 options.add_argument('-headless') # FirefoxのWebDriverオブジェクトを作成する。 ## driver = Firefox(options=options) driver = Firefox(options=options,capabilities=firefox_capabilities) # Googleのトップ画面を開く。 driver.get('https://www.google.co.jp/') # タイトルに'Google'が含まれていることを確認する。 assert 'Google' in driver.title ## # Full screen page_width = driver.execute_script('return document.body.scrollWidth') page_height = driver.execute_script('return document.body.scrollHeight') driver.set_window_size(page_width, page_height) # スクリーンショットを撮る。 driver.save_screenshot('firefox_search_results.png') driver.quit() # ブラウザーを終了する。 ! サンプル 引数付き( うまく動かない ) from selenium.webdriver import Firefox, FirefoxOptions from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC options = FirefoxOptions() # ヘッドレスモードを有効にする(次の行をコメントアウトすると画面が表示される)。 options.add_argument('-headless') # FirefoxのWebDriverオブジェクトを作成する。 driver = Firefox(options=options) # Googleのトップ画面を開く。 driver.get('https://www.google.co.jp/') # タイトルに'Google'が含まれていることを確認する。 assert 'Google' in driver.title # 検索語を入力して送信する。 input_element = driver.find_element_by_name('q') input_element.send_keys('Python') input_element.send_keys(Keys.RETURN) # 検索結果の要素が表示されるまで待つ。 WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'h3 > a'))) # スクリーンショットを撮る。 driver.save_screenshot('firefox_search_results.png') # 検索結果を表示する。 for a in driver.find_elements_by_css_selector('h3 > a'): print(a.text) print(a.get_attribute('href')) driver.quit() # ブラウザーを終了する。 ! PROXY from selenium import webdriver from selenium.webdriver.common.proxy import Proxy, ProxyType options = webdriver.ChromeOptions() options.add_argument('headless') desired_caps = options.to_capabilities() prox = Proxy() prox.proxy_type = ProxyType.MANUAL prox.http_proxy = "167.99.1.61:80" prox.add_to_capabilities(desired_caps) browser = webdriver.Chrome(desired_capabilities=desired_caps) ! Tips https://qiita.com/orangain/items/6a166a65f5546df72a9d https://qiita.com/orangain/items/6a166a65f5546df72a9d https://qiita.com/ryuichi1208/items/5d400f13a52fe28f4490 https://stackoverflow.com/questions/43960301/using-http-proxy-with-selenium-geckodriver https://qiita.com/orangain/items/6a166a65f5546df72a9d http://woodcnc300.blog.fc2.com/page-16.html https://end0tknr.hateblo.jp/entry/20190503/1556884037