对于使用Python的Selenium,如何获取它以按CTRL,SHIFT,i? [英] For Selenium with Python, how can i get it to press CTRL, SHIFT, i?

查看:89
本文介绍了对于使用Python的Selenium,如何获取它以按CTRL,SHIFT,i?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习Python类,这是我第一次编程,除了一点点HTML.我正在尝试为Instagram编写脚本,并希望能够将Chrome浏览器置于移动视图中.所以我的想法是先打开开发人员工具(CTRL + SHIFT + i),然后再移动(CTRL + SHIFT + m).如何让Selenium用Python代码做到这一点?

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


推荐答案

请尝试使用 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()

进口应为

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

这篇关于对于使用Python的Selenium,如何获取它以按CTRL,SHIFT,i?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆