Skip to content

Instantly share code, notes, and snippets.

@thefloodshark
Created February 27, 2021 05:03
Show Gist options
  • Save thefloodshark/d1fa01dd86cc8fdc5b9b9a8bbec9ff99 to your computer and use it in GitHub Desktop.
Save thefloodshark/d1fa01dd86cc8fdc5b9b9a8bbec9ff99 to your computer and use it in GitHub Desktop.
#Selenium written in Python
#Changes the user agent to search Bing as if on a mobile device (from the desktop)
#Problems with finding element and clicking to log into Bing ; Python mouse click events can fix this
#Work in progress - can likely replace the first sign-in attempt with a mouse click event and then remove the next bit of code that opens the sidebar and signs in
#Can streamline with dictionary/list/tuple/keys
#old method used fake_useragent
import time
from selenium import webdriver
from pynput.mouse import Button, Controller
from selenium.webdriver.chrome.options import Options
#from fake_useragent import UserAgent
from selenium.webdriver.common.keys import Keys
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
#ua = UserAgent()
#userAgent = ua.random
#print(userAgent)
options.add_argument(f'user-agent={"Mozilla/5.0 (Linux; Android 8.1.0; Pixel Build/OPM4.171019.021.D1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.109 Mobile Safari/537.36 EdgA/42.0.0.2057"}')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\_Coding\Selenium\chromedriver.exe')
mouse = Controller()
driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent":"Mozilla/5.0 (Linux; Android 8.1.0; Pixel Build/OPM4.171019.021.D1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.109 Mobile Safari/537.36 EdgA/42.0.0.2057", "platform":"Windows"})
driver.get('https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=13&ct=1522552641&rver=6.7.6631.0&wp=MBI&wreply=https%3a%2f%2fwww.bing.com%2fsecure%2fPassport.aspx%3frequrl%3dhttps%253a%252f%252fwww.bing.com%252f%253fwlexpsignin%253d1&lc=1033&id=264960&pcexp=false&CSRFToken=94467ae5-f34c-42a8-be9c-964caff9ac54&aadredir=1')
print("Page Title is : %s" %driver.title)
element = driver.find_element_by_xpath("//input[@class='form-control ltr_override input ext-input text-box ext-text-box' and @name='loginfmt']")
element.click()
element.clear()
element.send_keys("[email protected]")
time.sleep(2)
driver.find_element_by_id('idSIButton9').send_keys("\n")
time.sleep(1.6)
password = driver.find_element_by_xpath("//input[@class='form-control input ext-input text-box ext-text-box' and @name='passwd']")
password.click()
password.clear()
password.send_keys("YOURPASSWORD")
time.sleep(1.5)
driver.find_element_by_id('idSIButton9').send_keys("\n")
time.sleep(2)
driver.find_element_by_id('mHamburger').send_keys("\n")
time.sleep(1.5)
mouse.position = (1722,213)
mouse.click(Button.left, 1)
time.sleep(2)
element = driver.find_element_by_id("sb_form_q")
time.sleep(0.5)
element.send_keys("car")
time.sleep(0.5)
element.send_keys(Keys.RETURN)
time.sleep(1.7)
element = driver.find_element_by_name("q")
element.clear()
element.send_keys("ip")
element.send_keys(Keys.RETURN)
time.sleep(1)
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment