#python #py #www #http #automation #automation #pip #pip3 #chromedriver #chrome #selenium #webdriver Prerequisites: ``` pip3 install selenium ``` download chromedriver: ( [https://chromedriver.chromium.org/](https://chromedriver.chromium.org/) ) place the exe in %windir% (for windows users) script examples: #EXAMPLE 1 ----------------------------------------- ``` from selenium import webdriver driver = webdriver.Chrome() driver.get('https://youtube.com') ``` ----------------------------------------- #EXAMPLE 2 ----------------------------------------- ``` from selenium import webdriver driver = webdriver.Chrome() driver.get('https://youtube.com') ``` # Launch chrome # goto youtube.com # Press F12 to see the elements of the page. # rightclick on the element (this time it is the searchbox on the youtube page # Click Inspect Element # The rightpane will jump to the searchbox element # rightclick on the element and select copy xpath # the xpatch needs to be stores in a variable. In this case it is named searchbox: # the copied xpath is //*[@id="search"] searchbox = driver.find_element_byxpath('//*[@id="search"]') # use the send_keys method to sendkeys in the searchbox variable searchbox.send_keys('Jan van Dale') # To actually simulate the click the correct button to actually search. (right next to the searchbox, where can enter the text to search for. # rightclick on the element (this time it is the searchbox on the youtube page # Click Inspect Element # The rightpane will jump to the searchbox element # rightclick on the element and select copy xpath # the xpath needs to be the button on which the click should simulated. This the xpath is called: //*[@id="search-icon-legacy"]/yt-icon # It will be stored in the variable SearchButton SearchButton = driver.find_element_byxpath('//*[@id="search-icon-legacy"]/yt-icon') # fire up a method called click on the var SearchButton SearchButton.click() # THE ABOVE EXAMPLE MIGHT CAUSE ERRORS # EXAMPLE 3 is more reliable ----------------------------------------- #EXAMPLE 3 ----------------------------------------- ``` from selenium import webdriver from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains driver = webdriver.Chrome() url = '[http://www.youtube.com](http://www.youtube.com)' driver.get(url) driver.maximize_window() wait = WebDriverWait(driver, 20) print(driver.title) element = wait.until(EC.presence_of_element_located((By.XPATH, "//form[@id='search-form']//div[@id='container']//div[@id='search-input']//input[@id='search']"))) actionChains = ActionChains(driver) actionChains.move_to_element(element).click().perform() element.send_keys("Jan van Dale") element.submit() # actionChains.move_to_element(element).send_keys("Test",Keys.RETURN).perform() driver.maximize_window() ``` ----------------------------------------- #EXAMPLE 3 ``` # The example below doesn't work, but that is because of change in de gui of Instagram. But the code is correct! ----------------------------------------- from selenium import webdriver from time import sleep from secrets import pw class InstaBot:     def __init__(self, username, pw):         self.driver = webdriver.Chrome()         self.username = username         self.driver.get("https://instagram.com")         sleep(2)         self.driver.find_element_by_xpath("//a[contains(text(), 'Log in')]")\             .click()         sleep(2)         self.driver.find_element_by_xpath("//input[@name=\"username\"]")\             .send_keys(username)         self.driver.find_element_by_xpath("//input[@name=\"password\"]")\             .send_keys(pw)         self.driver.find_element_by_xpath('//button[@type="submit"]')\             .click()         sleep(4)         self.driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]")\             .click()         sleep(2)     def get_unfollowers(self):         self.driver.find_element_by_xpath("//a[contains(@href,'/{}')]".format(self.username))\             .click()         sleep(2)         self.driver.find_element_by_xpath("//a[contains(@href,'/following')]")\             .click()         following = self._get_names()         self.driver.find_element_by_xpath("//a[contains(@href,'/followers')]")\             .click()         followers = self._get_names()         not_following_back = [user for user in following if user not in followers]         print(not_following_back)     def _get_names(self):         sleep(2)         sugs = self.driver.find_element_by_xpath('//h4[contains(text(), Suggestions)]')         self.driver.execute_script('arguments[0].scrollIntoView()', sugs)         sleep(2)         scroll_box = self.driver.find_element_by_xpath("/html/body/div[3]/div/div[2]")         last_ht, ht = 0, 1         while last_ht != ht:             last_ht = ht             sleep(1)             ht = self.driver.execute_script("""                 arguments[0].scrollTo(0, arguments[0].scrollHeight);                 return arguments[0].scrollHeight;                 """, scroll_box)         links = scroll_box.find_elements_by_tag_name('a')         names = [name.text for name in links if name.text != '']         # close button         self.driver.find_element_by_xpath("/html/body/div[3]/div/div[1]/div/div[2]/button")\             .click()         return names my_bot = InstaBot('janvandalemusic_', pw) my_bot.get_unfollowers() ``` Zie ook [[PYTHON, Installing PIP]] Zie ook [[CHROME, Experimental Settings]] Zie ook [[Cool Google Searches]] Zie ook [[PYTHON, PY to EXE]]