Reputation: 11219
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)
Upvotes: 1
Views: 182
Reputation: 500
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 ...
Upvotes: 0
Reputation: 33335
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.
Upvotes: 2