Python Forum

Full Version: [PyCharm] Any Features To Make Programming Easier / Faster?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Say I have a script that does the following:

  1. Navigates to a website in Selenium
  2. Logs in
  3. Clicks category
  4. Navigates to post an entry

It takes 20-30 seconds for this script to complete.

There's still many more actions I need to code in and testing becomes a chore because every time I want to test a new section of code the whole thing has to repeat. This burns 20-30 seconds every time and gets worse as the script gets longer.

Is it possible to get PyCharm to only load a new section of code into the already open selenium browser?

For example, I just want to test if this one line works, but I don't want to have to replay the entire script:

driver.find_element_by_xpath("//div[@class='new_element']").click()
Any way to do that?
No, kind of. About the best you can do is create a 'scratch' file, write the minimum amount of code to run your test and once you have all the bugs worked out, copy it to your main script. Though you can edit a running script (with any editor) it will probably lead to some rather spectacular failures. Even though PyCharm allows you to edit a file while it is running, it is definitely a bad idea to do so. Better to stop the program, make your changes, then restart.
You're the programmer, and you control what is and isn't executed.
PyCharm is just a dumb IDE and doesn't care about what it dose.
you can however set breakpoints in the code and run in debugger.
Code will stop at breakpoint.
This is not an issue with your IDE, but an issue of efficiency.

Quote:There's still many more actions I need to code in and testing becomes a chore because every time I want to test a new section of code the whole thing has to repeat. This burns 20-30 seconds every time and gets worse as the script gets longer. time.sleep is shown a lot mainly for example and to simplify the example. But if you are trying to actually cut down on time, then you should be using selenium wait.
How are you delaying loads? Are you using time.sleep() or selenium wait? The latter can speed up over the first. You can waste a lot of time using time.sleep that the computer doesnt need to wait for.

If you are logging in, you must be logged in to see what proper content is there. For example a button might not exist on the page if you are not logged in. So you cannot just cut to the end. However you can remove the login process by saving cookies.

This would be an example
import pickle
from selenium import webdriver 

driver = webdriver.Firefox()
driver.get(URL)
#login here
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
on your next session
import pickle
from selenium import webdriver 

driver = webdriver.Firefox()
driver.get(URL)
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    driver.add_cookie(cookie)
Then if the post entry gives you a unique link for example, you just load that (assuming that is your final destination).