2

Selenium WebDriver, using Chrome and Powershell. I'm trying to choose and click the 'en' value, from the drop down of the language menu.

Here is the HTML Code:

<select id="language_select" name="language_select" class="form-control" onchange="ls.change();">
<option value="en" title="English">English</option>
<option value="fr" title="French">French</option>
</select>

Using java, that code will be as follow:

WebElement langmenu = driver.findElement(By.id("language_select"));
Select selectMonth = new Select(langmenu);
selectMonth.selectByVisibleText("en");

But I'm struggling to accomplish the same using Powershell, So far, I've manage to understand how to search by Id and store it in a variable, using Powershell:

$ChromeDriver = New-Object -TypeName OpenQA.Selenium.Chrome.ChromeDriver

$ChromeDriver.Navigate().GoToURL('https://myserver/login/')

$langmenu = $ChromeDriver.FindElementById("language_select")

But I don't understand how to create a new select object, and choose the 'en' value, so I can use the selectByVisibleText or selectByIndex methods on the the select object.

3
  • Just curious why powershell? Also you can execute C# from within powershell. Using C# will make it easier to find examples howto use Selenium: blog.adamfurmanek.pl/2016/03/19/… Commented Dec 29, 2020 at 15:28
  • only for my learning purpose, as i'm trying to shift my QA tests, to run with PowerShell.
    – edwio
    Commented Dec 29, 2020 at 16:06
  • 1
    Feels like a real small niche. One that is poorly documented and which doesn't have a lot of examples online. The latter makes self-learning even harder. I would rather write the tests in a real programming language and only start the test-case from Powershell. Commented Dec 29, 2020 at 16:51

2 Answers 2

1

Since Selenium has bindings for C# as well, it is fairly doable to be able to construct your Powershell code depending on those bindings, as @Niels pointed out in the other answer.

But if your goal is to run Selenium tests by using Powershell, I would suggest taking a look at the following library: selenium-powershell

An example for what you're trying to achieve:

# Start a Chrome
$Driver = Start-SeChrome

# Open URL
Enter-SeUrl https://myserver/login/ -Driver $Driver

# Find element by id 
$Element = Find-SeElement -Driver $Driver -Id "language_select"

# Select by value
Set-SeSelectValue -By Value -value 'en' -Element $Element

While I didn't personally played too much with it, it seems pretty well documented and, equally important in my opinion, actively maintained.

3
  • can you please explain the difference between using the PowerShell module for selenium, vs interacting with .NET objects in PowerShell, like I'm currently doing? is there any pros/cons between the two? if I'm not mistaken the PowerShell module need to be maintained, while the .NET approach is natively using the WebDriver dll files.
    – edwio
    Commented Dec 30, 2020 at 17:46
  • 1
    [1] By going on the approach to create Selenium tests with Powershell you're already stepping into a niche territory. In my point of view, by using an already implemented framework for Powershell, if it's solving your request, is by any means no different than the reason you're choosing to use Selenium and not start implementing everything from scratch. I don't consider using the C# bindings a wrong path to take, however your tests will become incredibly bloated due to the multitude of DLLs that you need to import. And as your tests grow, you'll need to extract methods for common steps
    – Cosmin
    Commented Dec 31, 2020 at 9:33
  • 1
    [2] otherwise you will have a lot of duplicate code. This, of course, depends on how complex your AUT is, how much of it can be covered using out-of-the-box C# Selenium and how much custom code you need to write in order to achieve your goals. On the long run, a really important aspect that you need to take into consideration is the maintainability of your solution.
    – Cosmin
    Commented Dec 31, 2020 at 9:47
0

I think something like this might work:

$SelectElement = [OpenQA.Selenium.Support.UI.SelectElement]::new($langmenu)
$SelectElement.SelectByVisibleText("en")

Example from Powershell Selenium select a dropdown menu with FindElementByCssSelector.

2
  • Do i need to add another .dll file, in order to use OpenQA.Selenium.Support.UI.SelectElement ? i don't think this namespace exist on OpenQA.Selenium.Chrome.ChromeDriver. am i wrong?
    – edwio
    Commented Dec 29, 2020 at 16:00
  • Yes, I think you are right. This page describes how to get the Support DLL's from Nuget: alkanesolutions.co.uk/2018/12/06/… Commented Dec 29, 2020 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.