Day 048
Udemy Python Bootcamp Day 048
Selenium WebDriver Browser
One of the things that we've really been limited by is we can't actually use all the capabilities that browsers can do. To do that, we're going to use Selenium WebDriver.
Selenium WebDriver is basically allows us to automate the browser, get the browser to do things automatically depending on a script or a piece of code that we write.
This is going to enable us to type as well as click as well as scroll. Basically anything that a human pretty much can do on a website, you can do using a Selenium driven browser. It's kind of like we're building a robot and telling it what to do on a browser. And selenium is the tool that allows the robot to interact and communicate with the browser.
Chrome Driver
We've got our Selenium package which contains code for us to be able to interact with browsers. Now it can interact with the Chrome browser which is what we're choosing to do.
And we're going to need some sort of a bridge that the selenium code to work with the Chrome browser. And this bridge is provided by the Chrome driver.
Chrome Driver will tell selenium how to work woth the latest version of these browsers.
Set Up Selenium
from selenium import webdriver
chrome_driver_path = "C:/Users/OWNER/chromedriver"
driver = webdriver.Chrome(executable_path=chrome_driver_path)
driver.get("https://www.amazon.com")
driver.close()
driver.quit()
.close()
: just close a single tab, the active tap where you've opened up particular page. .quit()
: quit the entire browser.The XPath
The XPath is a way of locating a specific HTML element by a path structure. So, we can also express the navigation to a particular element drilling down from the top of the tree to a particular node using the XPath.
e.g.
'//*[@id="corePrice_desktop"]/div/table/tbody/tr[2]/td[2]/span[1]/span[2]'
Find and Select Elements on a Website with Selenium
Extract the price of the product on amazon.com
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
service = Service("C:/Users/OWNER/chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get("https://www.amazon.com/Instant-Pot-Plus-60-Programmable/dp/B01NBKTPTS?ref_=Oct_d_otopr_d_3117954011&pd_rd_w=YCvdx&pf_rd_p=8f269526-780e-4f8e-9daa-56fd73859d4c&pf_rd_r=GANQAAZ3D5TFK0J8XS6S&pd_rd_r=3b49cd37-6d52-42f2-8d40-40ac1a4b2aff&pd_rd_wg=aNbGT&pd_rd_i=B01NBKTPTS")
price = driver.find_element(By.CLASS_NAME, "a-offscreen")
# print("Check", price.tag_name)
print(price.get_attribute('innerHTML'))
driver.quit()
方法は変更され、天才天才天才回答者の助けを得た.Use Selenium to Scrape Website Date
By.CLASS_NAME
driver.get("https://www.python.org/")
event_times = driver.find_elements(By.CLASS_NAME, "event-widget time")
event_names = driver.find_elements(By.CLASS_NAME, "event-widget li a")
events = {}
for n in range(len(event_times)):
events[n] = {
"time": event_times[n].text,
"name": event_names[n].text
}
print(events)
driver.quit()
By.CSS_SELECTOR
driver.get("https://en.wikipedia.org/wiki/Main_Page")
article_count = driver.find_element(By.CSS_SELECTOR, "#articlecount a")
print(article_count.text)
driver.quit()
Automate Filling Out Forms and Clicking with Selenium
.click()
all_portals = driver.find_element(By.LINK_TEXT, "All portals")
all_portals.click()
send_keys()
from selenium.webdriver.common.keys import Keys
search = driver.find_element(By.NAME, "search")
search.send_keys("Python")
search.send_keys(Keys.ENTER)
automate sign in
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
service = Service("C:/Users/OWNER/chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get("http://secure-retreat-92358.herokuapp.com/")
first_name = driver.find_element(By.NAME, "fName")
first_name.send_keys("awesome")
last_name = driver.find_element(By.NAME, "lName")
last_name.send_keys("kim")
email = driver.find_element(By.NAME, "email")
email.send_keys("[email protected]")
# email.send_keys(Keys.ENTER)
# OR
submit = driver.find_element(By.CSS_SELECTOR, "form button")
submit.click()
Game Playing Bot
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
service = Service("C:/Users/OWNER/chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get("http://orteil.dashnet.org/experiments/cookie/")
#Get cookie to click on.
cookie = driver.find_element(By.CSS_SELECTOR, "#cookie")
#Get upgrade item ids.
items = driver.find_elements(By.CSS_SELECTOR, "#store div")
item_ids = [item.get_attribute("id") for item in items]
timeout = time.time() + 5
five_min = time.time() + 60*5 # 5minutes
while True:
cookie.click()
# Every 5 seconds:
if time.time() > timeout:
# Get all upgrade <b> tags
all_prices = driver.find_elements(By.CSS_SELECTOR, "#store b")
item_prices = []
# Convert <b> text into an integer price.
for price in all_prices:
element_text = price.text
if element_text != "":
cost = int(element_text.split("-")[1].replace(",", ""))
item_prices.append(cost)
# Create dictionary of store items and prices
cookie_upgrades = {}
for n in range(len(item_prices)):
cookie_upgrades[item_prices[n]] = item_ids[n]
# Get current cookie count
money_element = driver.find_element(By.ID, "money").text
if "," in money_element:
money_element = money_element.replace(",", "")
cookie_count = int(money_element)
# Find upgrades that we can currently afford
affordable_upgrades = {}
for cost, id in cookie_upgrades.items():
if cookie_count > cost:
affordable_upgrades[cost] = id
# Purchase the most expensive affordable upgrade
highest_price_affordable_upgrade = max(affordable_upgrades)
print(highest_price_affordable_upgrade)
to_purchase_id = affordable_upgrades[highest_price_affordable_upgrade]
driver.find_element(By.ID, to_purchase_id).click()
# Add another 5 seconds until the next check
timeout = time.time() + 5
# After 5 minutes stop the bot and check the cookies per second count.
if time.time() > five_min:
cookie_per_s = driver.find_element(By.ID, "cps").text
print(cookie_per_s)
break
無駄にupgrade item
万個書いてあるclick
まで、5秒の実現方法が分からずうろうろしていたので、そこに落ちて…
なんといっても、ゲーム開発は私には向いていないような・・・
ゲームを実施するだけの項目はめちゃくちゃだ.
アルゴリズムが作れないという説と脈々と受け継がれているかもしれませんが...
Reference
この問題について(Day 048), 我々は、より多くの情報をここで見つけました https://velog.io/@awesomee/Day-048テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol