2

I have a powershell script that is running some automation for me, as part of this I have written a script in python that utilizes selenium to open a web page, input data and exit. I need to do the same thing for another site but it has a login page. From my powershell script I am prompting the user for Name and Pwd, saving this to a variable. The part I am struggling with is passing their input into the python send_keys function. Is this even possible?

My powershell code is $usernme = Read-Host -Prompt 'Input Username'

Then in the python script

user = browser.find_element_by_xpath("""/elementname""")
user.send_keys('$usernme')

Which only sends $usernme to the field I am writing to and not the value of $usernme. Is it possible to grab this value from the powershell script and use it when I call the python script from within powershell.

2 Answers 2

4

You can run the python script inside the powershell script, and extract the command argument with sys.argv[1]. You can have a look at sys.argv for more specifics.

test.ps1:

$username = Read-Host -Prompt 'Input Username'
python script.py $username

script.py:

import sys
print("Your Username:", sys.argv[1])

Usage:

PS> .\test.ps1
Input Username: myusername
Your Username: myusername

Specifically to your code, you would change user.send_keys('$usernme') to user.send_keys(sys.argv[1]).

Sign up to request clarification or add additional context in comments.

Comments

1

You can also try to run your selenium automation right from powershell using C# driver. Extract WebDriver.dll (from Selenium.WebDriver.3.14.0.nupkg) and put it in the folder with you PS script, along with geckodriver.exe (if using firefox). Below is a sample script you can use as a starting point:

Add-Type -Path ".\WebDriver.dll"
# put geckodriver.exe in the same folder

$driver = [OpenQA.Selenium.Firefox.FirefoxDriver]::new()

$driver.manage().timeouts().ImplicitWait = [timespan]::FromSeconds(5)

$driver.Navigate().GoToUrl("someurl")

$driver.FindElementByCssSelector(".some-button").Click()  

$driver.quit()

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.