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

我在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:UtilityBrowserDriverschromedriver.exe')
driver.get('https://www.google.co.in')
print("Page Title is : %s" %driver.title)
driver.quit()

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

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

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

希望这对你有所帮助!

最新更新