Selenium - Powershell 如何打开特定的 Firefox 配置文件

问题描述 投票:0回答:2

我对 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()
selenium webdriver
2个回答
0
投票

我将 selenium 与 python 一起使用。检查下面的代码补丁,也许你可以将它转换成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


对于火狐浏览器:

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);

Chrome

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);

希望这对您有所帮助!


0
投票

我按照此博文中的说明设置 Selenium。因此我的解决方案不是使用您提到的 Selenium 模块。

$seleniumPath = 'C:\selenium'
$webDriverDllFileName = 'WebDriver.dll'
$firefoxProfilePath = 'C:\Users\gpunktschmitz\AppData\Roaming\Mozilla\Firefox\Profiles\f6k3p67h.default-release'

#-

$webDriverDllFilePath = Join-Path -Path $seleniumPath -ChildPath $webDriverDllFileName
Add-Type -LiteralPath $webDriverDllFilePath -EA Stop

$firefoxOptions = [OpenQA.Selenium.Firefox.FirefoxOptions]::new()
$firefoxOptions.AddArgument("-profile=$firefoxProfilePath")

$firefoxDriver = New-Object -TypeName OpenQA.Selenium.Firefox.FirefoxDriver -ArgumentList $firefoxOptions

$firefoxDriver.Navigate().GoToUrl('https://example.org')

Start-Sleep -Seconds 3

$firefoxDriver.Quit()
© www.soinside.com 2019 - 2024. All rights reserved.