首页 > 解决方案 > Selenium - Powershell 如何打开特定的 Firefox 配置文件

问题描述

我对 Powershell 的 selenium 有一些疑问。我正在尝试通过 powershell 在 selenium 中打开特定的 Firefox 配置文件,但 -Profile 开关不起作用。有没有办法设置允许地理位置弹出?如何打开多个标签?

    Install-Module Selenium
$Driver = Start-SeFirefox
Enter-SeUrl -Driver $Driver -Url "something.com"
Find-SeElement -Driver $Driver -XPath
$dropDown = $Driver.FindElementByXPath('//*[@id="dbName"]');
Invoke-SeClick -Element $dropDown
$dropdownOption = $Driver.FindElementByXPath("/html/body/form/div[3]/div[2]/div/div[1]/select/option[2]");
Invoke-SeClick -Element $dropdownOption
$Driver.FindElementByXPath('//*[@id="LoginId"]').SendKeys('abc')
$Driver.FindElementByXPath('//*[@id="LoginPwd"]').SendKeys('1234567')
$Driver.FindElementByXPath('//*[@id="Button_Login"]').Click()
Start-Sleep -s 5
$driver.SwitchTo().Frame(1)
$driver.findElementBylinkText("Attendance").click()
$Driver.FindElementByXPath("/html/body/form/div[6]/div[2]/div[1]/div/ul/li[3]/a").Click()
$Driver.FindElementByXPath('//*[@id="Remarks"]').SendKeys('In')
$Driver.FindElementByXPath('//*[@id="checkIn"]').click()
#Browser ask for Location Pop-up here.
$Driver.FindElementByXPath("/html/body/div/div/div/a[1]").click()
Start-Sleep -s 5
$Driver.FindElementByXPath("/html/body/div/div/div/a[1]").click()
$driver.SwitchTo().DefaultContent()
$Driver.FindElementByXPath('//*[@id="toggleProfile"]').Click()
Start-Sleep -s 900
$Driver.FindElementByXPath("/html/body/form/nav/ul/li[3]/div/ul/li[5]/a").Click()

标签: seleniumwebdriver

解决方案


我在 python 中使用 selenium。检查下面的代码补丁,也许您可​​以将其转换为 powershell

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

opt = Options()
opt.add_argument("--disable-infobars")
opt.add_argument("start-maximized")
opt.add_argument("--disable-extensions")
# Pass the argument 1 to allow and 2 to block
opt.add_experimental_option("prefs", { \
    "profile.default_content_setting_values.media_stream_mic": 1, 
    "profile.default_content_setting_values.media_stream_camera": 1,
    "profile.default_content_setting_values.geolocation": 1, 
    "profile.default_content_setting_values.notifications": 1 
  })

driver = webdriver.Chrome(chrome_options=opt, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://www.google.co.in')
print("Page Title is : %s" %driver.title)
driver.quit()

对于相应的浏览器,properties/options我还参考了以下内容。Selenium-Java您可以在启动浏览器时尝试使用这些参数,因为您无法与此元素交互,因为它不是WebElement

For firefox:

FirefoxProfile geoDisabled = new FirefoxProfile();
geoDisabled.setPreference("geo.enabled", false);
geoDisabled.setPreference("geo.provider.use_corelocation", false);
geoDisabled.setPreference("geo.prompt.testing", false);
geoDisabled.setPreference("geo.prompt.testing.allow", false);
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, geoDisabled);
driver = new FirefoxDriver(capabilities);

对于铬

ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("test-type");
options.addArguments("enable-strict-powerful-feature-restrictions");
options.addArguments("disable-geolocation");
cap.setCapability(ChromeOptions.CAPABILITY, options);
cap = cap.merge(DesiredCapabilities.chrome());
driver = new ChromeDriver(cap);

希望这对您有所帮助!


推荐阅读