1

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?

1 Answer 1

1

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"})
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response! Where would you add that code? I've tried it in a few places and the script won't even run :(
sorry for late reply, just remove this from your code: from fake_useragent import UserAgent, ua = UserAgent(), userAgent = ua.random, print(userAgent), and run, if it doesn't change the UserAgent, place my code under this code driver = webdriver.Chrome(chrome_options=options, executable_path=r'
Thanks again. I tried your suggestions, first removing the code you mentioned and running the script, then adding your code and running it. All variations technically start and run the automation with a mobile-looking UI, they just don't register as Edge/Mobile searches on Bing's end like the code in my original post. My original code stopped working a few weeks ago - maybe Edge or Chrome updates are the culprit?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.