1

Since Python is mostly a runtime language, would it be possible through Selenium to have something like 'wait for developer input'. In my case it would be way more effective to test code live on a web page (like offset or scrolling) than to relaunch the website each time the code does not work as expected.

Something like

(...)
driver.get(url)

while True:
    command = wait_for_python_code()
    # developer inputs 'elem = driver.find_element_by_class_name('myclass')\nprint(elem.text)'
    # selenium prints content of myclass on the go

So basically dynamically allow the user (developer) to type any arbitrary python code, that will be fed to selenium.

NB: I am not looking for an answer to get the class_name provided by the user input, but the whole code block that could be anything (and this is for internal use so no worries about security flaws)

2 Answers 2

2

You can use the input() function to accept keyboard input.

If you just want to find an element with a given class, you can use this:

while True:
    klass = input("Enter the element class name: ")
    elem = driver.find_element_by_class_name(klass)
    print(elem.text)

However if you want to allow the user to type any arbitrary python code, it is a lot more complicated.

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

1 Comment

I am looking for the latter one sorry!
0

One very handy approach to debugging a Selenium script is to return the driver object to your prompt, then you can use the driver as you would in a script, but you have access to it in your interactive python shell:

# mycode.py

from selenium import webdriver
import time

def get_driver():
    options = webdriver.ChromeOptions()
    driver = webdriver.Chrome(chrome_options=options)
    return driver

You can now open a python shell and run this code manually

> from mycode import get_driver
> driver = get_driver

You can now debug the driver as needed:

> driver.get('https://python.org')
> driver.find_element_by_class_name('div#ICanDoAnyThingIWant')

# etc ...

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.