I wrote it in Ruby and Python, but since I decided to write it in Python in the second half, Ruby became only a simple part ... Please note that the Ruby part is an additional note.
python
selenium
pip install selenium
chromewebdriver Because it was a mac
brew install chromedriver
Linux is below? (I don't know because I haven't tried it ...)
sudo apt-get install chromium-browser
Referenced page http://stackoverflow.com/questions/8255929/running-webdriver-chrome-with-selenium
A simple example of accessing the Google homepage, waiting 10 seconds and closing
sample.py
from selenium import webdriver
from time import sleep
browser = webdriver.Chrome()
browser.get('http://google.com')
sleep(10)
browser.close()
login.py
#Find the part where id is email
mail = browser.find_element_by_id('email')
#Find the part where id is pass
pass_wd = browser.find_element_by_id('pass')
#Enter email
mail.send_keys('[email protected]')
#Enter pass
pass_wd.send_keys('password')
#Send
pass_wd.submit()
In the case of Ruby, it is okay to leave it as it is, but in the case of Python, this PopUp makes it impossible to execute the program, so set chrome_options in advance.
Change before
browser = webdriver.Chrome()
After change
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
browser = webdriver.Chrome(chrome_options=chrome_options)
Scroll to the top of the page
browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
links = myelement.find_elements_by_xpath(".//a")
links = myelement.find_elements_by_xpath("//a")
If you get a Link with any of the above information, use get_attribute ('href')
to get the URL
urls = [ link.get_attribute('href') for link in links]
http://www.takunoko.com/blog/pythonselenium%E3%81%A7twitter%E3%81%AB%E3%83%AD%E3%82%B0%E3%82%A4%E3%83%B3%E3%81%97%E3%81%A6%E3%81%BF%E3%82%8B/ Easy login
http://selenium-python.readthedocs.io/faq.html --Scroll --take a link
ruby
selenium-webdriver gem
gem install selenium-webdriver
chrome driver
After downloading and unzipping the chromedriver
Check the location of ruby with which ruby
and move to it
If you are using rbenv, you can use the following command
mv chromedriver ~/.rbenv/shims
require "selenium-webdriver"
driver = Selenium::WebDriver.for :chrome
driver.navigate.to "http://google.com"
driver.quit
## type email
element = driver.find_element(:id, 'email')
element.send_keys '[email protected]'
# type password
element = driver.find_element(:id, 'pass')
element.send_keys 'password'
# submit the form
element.submit
Now you can get the screen after logging in.
http://shoprev.hatenablog.com/entry/2014/04/14/210529 See ChromeDriver settings and simple code section
https://gist.github.com/huangzhichong/3284966 See here for details yesterday
Recommended Posts