尝试在osx/safari上使用selenium实现逻辑过程的自动化。但是得到了康斯坦丁

2024-06-17 15:02:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试selenium登录到某个站点,但经常出错。在

selenium.common.exceptions.WebDriverException: Message: Could not create a session: You must enable the 'Allow Remote Automation' option in Safari's Develop menu to control Safari via WebDriver.

我已经从浏览器启用了远程自动化。下面是使用的代码。在

from selenium import webdriver

#Following are optional required
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException

    baseurl = "xxxxx"
    username = "xxxx"
    password = "xxxx"

    xpaths = { 'usernameTxtBox' : "//*[@id='user_login']",
               'passwordTxtBox' : "//*[@id='user_pass']",
               'submitButton' :   "//*[@id='wp-submit']"
             }

    mydriver = webdriver.Safari()
    mydriver.get(baseurl)
    mydriver.maximize_window()

    #Clear Username TextBox if already allowed "Remember Me" 
    mydriver.find_element_by_xpath(xpaths['usernameTxtBox']).clear()

    #Write Username in Username TextBox
    mydriver.find_element_by_xpath(xpaths['usernameTxtBox']).send_keys(username)

    #Clear Password TextBox if already allowed "Remember Me" 
    mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).clear()

    #Write Password in password TextBox
    mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).send_keys(password)

    #Click Login button
    mydriver.find_element_by_xpath(xpaths['submitButton']).click()

Tags: infromimportbyseleniumpasswordelementcommon