对于使用Python的Selenium,如何使其按CTRL、SHIFT和i键?
我刚刚开始学习Python课程,这是我第一次接触编程,而不是一点HTML。我正在尝试为Instagram编写一个脚本,希望能够将Chrome浏览器引入移动视图。所以我的想法是先打开开发工具(CTRL+SHIFT+i),然后再打开移动工具(CTRL+SHIFT+m)。如何让Selenium使用Python代码来实现这一点对于使用Python的Selenium,如何使其按CTRL、SHIFT和i键?,python,selenium,selenium-chromedriver,instagram,hotkeys,Python,Selenium,Selenium Chromedriver,Instagram,Hotkeys,我刚刚开始学习Python课程,这是我第一次接触编程,而不是一点HTML。我正在尝试为Instagram编写一个脚本,希望能够将Chrome浏览器引入移动视图。所以我的想法是先打开开发工具(CTRL+SHIFT+i),然后再打开移动工具(CTRL+SHIFT+m)。如何让Selenium使用Python代码来实现这一点 String selectAll = Keys.chord(Keys.ALT, Keys.SHIFT,"z"); driver.findElement(By.tagName(
String selectAll = Keys.chord(Keys.ALT, Keys.SHIFT,"z");
driver.findElement(By.tagName("html")).sendKeys(selectAll);
我试图修改它,使其工作,但它没有。我需要导入一些东西才能使上面的块工作吗
下面是我所拥有的代码,在现有代码运行后,我正在尝试进入移动模式
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()```
钥匙看起来有点粗糙 请尝试以下方法:
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())
参考:请尝试使用
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
了解如何使用chrome DriverTanks模拟移动设备的响应。我需要导入一些东西才能让它工作吗?我试图添加这段代码,我得到一个回溯说“NameError:name'ActionChains'未定义”,再次感谢。我现在得到这个错误。“actions=ActionChains(驱动程序)名称错误:未定义名称‘驱动程序’”是否已设置webdriver驱动程序实例?为此,您需要驱动程序导入这些是我从selenium导入的导入
从time导入webdriver从selenium导入sleep.webdriver.common.keys从selenium.webdriver.common.action\u chains导入ActionChains根据您的问题更新答案谢谢您的建议。我是否需要在移动模式下运行整个脚本才能工作,或者我是否可以启动Instagram,以桌面格式登录,然后使用此代码更改为移动仿真模式?我不确定,但我认为您可以定义两个驱动程序,并根据您的需要使用桌面或移动。