DEV Community 👩‍💻👨‍💻

Discussion on: How To Automate Your Browser using Selenium Webdriver and Python

Collapse
 
labibllaca profile image
labibllaca

Hi Arvin,
congrats to your cool article. Do you know how to get value from js and than put that text to the webpage through the send_keys() ?

Here an example of mine:

self.driver = webdriver.Chrome()
usrname = self.driver.find_element_by_xpath('//*[@id="username"]')
loginUser = self.driver.execute_script("myName = prompt('Your name please: '); return {myName};")
usrname.send_keys(loginUser)
  • so first an prompt appears where you can give the login credentials and after that selenium works with that
  • but the prompt is only though javascript accesable (as far as I know) therefore my strange question :D

Cheers,
Labi

Collapse
 
arvindmehairjan profile image
Arvind Mehairjan Author • Edited on

Good question.

To get text from a webpage you can use getText()

For example:
get_text_value = driver.find_element_by_xpath("your-xpath").getText()

With the value you found you can switch to the prompt message.

You can do that by using the switchTo method. You can use the get_text_value in the prompt.
So something like this:

driver.switchTo().alert().sendKeys(get_text_value)

This is perhaps how you can solve it.

Hope it works for you! :)

Otherwise this doc can perhaps help you.

Collapse
 
labibllaca profile image
labibllaca

Oh wow thanks Arra, it looks very promising. I'll definitly going to try it. And the doc is just perfect for what I need to learn.

My question was kind of semi-automatation of an proces I'd like to create. For example the download of an specific pdf in a webpage that needs authentification. I looks like the pdf that this magazine-webpage provides comes every month and the xpath is changing by just an character (the one othe month of a year). By getting the desired number from an user that s/he puts i an prompt this character would be saved in the script and proceded with it.

Thanks again.

Thread Thread
 
arvindmehairjan profile image
Arvind Mehairjan Author • Edited on

No problem mate. Good luck with your project.