Day 048

AWESOMee·2022년 3월 22일
0

Udemy Python Bootcamp

목록 보기
48/64
post-thumbnail

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()

method 변경사항있어서 또 천재만재 질의응답분들의 도움을 받았음

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("awesome@email.com")

# 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초 구현하는 방법을 몰라서 헤맸고 거기서 드랍함..
아무래도 게임개발은 나랑 안맞는거 같어...
게임 구현하는 프로젝트만 하면 엉망진창이네
알고리즘을 못 짠다는 얘기와 아마 일맥상통하겠지..

profile
개발을 배우는 듯 하면서도

0개의 댓글