2

I have just started taking a Python class and is my first experience with programming, other than a little HTML. I am trying to write a script for Instagram and would like to be able to get the Chrome browser into a mobile view. So my thought is to open the developer tools (CTRL+SHIFT+i) and then mobile (CTRL+SHIFT+m) How can I get Selenium to do this with Python code?

String selectAll = Keys.chord(Keys.ALT, Keys.SHIFT,"z");


driver.findElement(By.tagName("html")).sendKeys(selectAll);

I tried to modify this to get it to work but it didn't. Would I need to import something for the above block to work?

Here is the code i have and am trying to get to mobile mode after the existing code runs.

from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

#mobile_emulation = { "deviceName": "iPhone 4" }

#chrome_options = webdriver.ChromeOptions()

#chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)

#driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',

                          #desired_capabilities = chrome_options.to_capabilities())

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

my_bot = InstaBot('username', 'password')

actions = ActionChains(driver)
actions.send_keys(Keys.CTRL, Keys.SHIFT, "i")
actions.perform()```


lemans455
  • 21
  • 1
  • 4
  • Have a look at [this answer](https://stackoverflow.com/a/25394143/11402024) for how to emulate a mobile device using the chrome driver – Roqux Jan 21 '20 at 21:36

2 Answers2

2

Please try to send keys with ActionChains

self.driver = webdriver.Chrome(executable_path=r"C:\path\to\chromedriver.exe")

actions = ActionChains(self.driver) 
    actions.send_keys(Keys.CTRL, Keys.SHIFT, "i")
    actions.perform()

Imports would be

from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.action_chains import ActionChains
Muzzamil
  • 2,823
  • 2
  • 11
  • 23
  • Thanks for the response. Would i have to import something to get this to work? I tried to add this code in and i am getting a traceback saying that "NameError: name 'ActionChains' is not defined" – lemans455 Jan 21 '20 at 22:21
  • Thanks again. I am now getting this error. "actions = ActionChains(driver) NameError: name 'driver' is not defined" – lemans455 Jan 21 '20 at 22:26
  • Have you set up webdriver driver instance ? For that you need driver import – Muzzamil Jan 21 '20 at 22:30
  • These are the imports that i have included ```from selenium import webdriver from time import sleep from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.action_chains import ActionChains``` – lemans455 Jan 21 '20 at 22:32
  • Updated answer as per your question – Muzzamil Jan 21 '20 at 22:44
  • I tried to update the imports according to your suggestion and it didn't work. ```actions = ActionChains(driver) NameError: name 'driver' is not defined``` from line 44 – lemans455 Jan 22 '20 at 05:44
  • Are you able to open browser? You have to provide chromedriver path in driver object. You need to download chromedriver and point its location in driver object. – Muzzamil Jan 22 '20 at 07:24
0

Keys looks a little bit hacky.

Please try with this:

from selenium import webdriver
mobile_emulation = { "deviceName": "Nexus 5" }
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',
                          desired_capabilities = chrome_options.to_capabilities())

Ref: https://chromedriver.chromium.org/mobile-emulation

Xyrus
  • 908
  • 10
  • 13
  • Thank you, for the suggestion. Would i need to run the whole script in mobile mode for this to work or can i pull up Instagram, login in desktop format and then change to mobile emulation mode with this code? – lemans455 Jan 21 '20 at 22:23
  • I'm not sure, but I think you can define two drivers and depends on your need use desktop or mobile. – Xyrus Jan 22 '20 at 19:08