Reputation: 811
I'm currently using Selenium to automate a Bing search via Chrome. I want the script to automate opening Chrome with the User Agent set to Edge Mobile. According to information in the Chrome devtools, the User-Agent should read:
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
When I try searching through Bing on a regularly opened Chrome whereby I manually change the User-Agent to Edge and Mobile, Bing acknowledges that I am performing searches via Edge and mobile (Bing can track where you searched). However, when I use my script, it does not recognize that the searches are via Edge & mobile.
This script used to work, but recently Bing won't recognize the changed User-Agent, even though the UI appears to change. Here is my full code:
import time
from selenium import webdriver
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')
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)
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("password")
time.sleep(2)
driver.find_element_by_id('idSIButton9').send_keys("\n")
time.sleep(2)
element = driver.find_element_by_id("sb_form_q")
element.send_keys("BLUE")
element.send_keys(Keys.RETURN)
time.sleep(2)
element = driver.find_element_by_name("q")
element.clear()
element.send_keys("FINISH")
element.send_keys(Keys.RETURN)
time.sleep(1)
driver.quit()
I believe all my packages and chromedriver are up to date. The interface opens and runs the searches when I use the script (and the UI seemingly changes to mobile), but Bing does not register this search as a mobile search nor an Edge search, for whatever reason. But it does work when I open up Chrome manually and change the User-Agent manually. I have another script that does the same thing without the User-Agent code (so just searching via regular Chrome desktop user agent) and Bing recognizes this as it should (as a desktop search).
Here is an error code I am receiving in the command prompt when running the above script, as well:
DevTools listening on ws://127.0.0.1:65529/devtools/browser/02af2e70-b10a-4d85-9d5d-01e4e828a911
Page Title is : Sign in to Bing
[17928:16612:0826/140206.701:ERROR:device_event_log_impl.cc(208)] [14:02:06.701] Bluetooth: bluetooth_adapter_winrt.cc:1074 Getting Default Adapter failed.
I simply want the script to search with the Edge & Android Mobile user agent, which it appears to be doing considering the Chrome UI changes to a mobile one in the test automation, but Bing doesn't recognize it as such. Any ideas?
Upvotes: 1
Views: 1551
Reputation: 23
add this to you code:
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"})
Upvotes: 1