Working on a program to automate some daily functions that I do using Selenium to interact with a browser.
I have the script working exactly as I want it to with one email account, however I have several I would like to automate. I would like it to be able to choose one email, complete the entirety of the code, loop back to the beginning, and continue from the beginning, but with my next account in the variable.
For example, my login data is stored like this:
emails=('email1#email.com',
'email2#email.com',
'email3#email.com',
)
myPassword=('passwordtext')
And is called during the login process in this way, just for reference:
for email in emails:
emailid=driver.find_element_by_name('email')
emailid.send_keys('email') # this is the variable that would need to change each time
password=driver.find_element_by_name('password')
password.click()
password.send_keys(myPassword)
password.send_keys(Keys.ENTER)
# then, several more work functions are completed using Selenium
Obviously, this doesn't work and leads to all of the email addresses in the variable being cycled through, and then the code continues. What I would like is for one email address to be selected, the rest of the program is executed, and then it loops back to the beginning and continues with the next email address in the variable.
I'm pretty sure the solution is a nested loop here and that this is a pretty basic question, but I can't get my head around how to set that up. Again, the rest of the program works perfectly, I'm just trying to figure out a way to loop through the program, changing only the login information, until the end of the variable. I hope this makes sense.
Everything that you want to run for each email address needs to be indented under your for email in emails: line.
There is no need for a nested loop.
for email in emails:
stuff_that_runs_once_per_email_address
stuff_that_runs_independent_of_email_address
Related
I need a run a function infinitely until we manually stop it. But after executing each loop we have to display the current loop result in the template. After displaying the result it continues to the next loop.
How to execute this process flow. Please advise.
In Django, the functions that are in the views.py file are called every time a request is made by the user.
Writting in views.py a infinite loop is just impossible but there is way to do what you want :
You can add in the template a js function to reload the page and request the page to your server. I don't know what you want to do in your infinite loop but you can use variables and increase every time the page is called.
If you want to refresh dynamically without refreshing the webpage every time : you can create a python file like "infiniteLoop.py" and edit a json/txt in media folder and a js function can check it every x sec.
Or if you want to do that properly, you can use node and socket.io to send the data to the user.
The 2nd choice is maybe the best, I hope it helps !
I've been getting a lot of spam emails from various colleges. I wrote a simple Python program to go through my emails, and find all the ones whose sender ends in .edu.
Then, to delete it, I though I was supposed to add the \\Deleted flag to those emails using: Gmail.store(id, "+FLAGS", "\\Deleted"). This did not work, and some of them didn't even disappear from the inbox.
After more research, I found that instead I had to use Gmail.store(id, "+X-GM-LABELS", "\\Trash").
So I updated the program to use that, but now it doesn't see any of the emails that I previously added the \\Deleted flag to.
Is there any way I can reset all those emails and then trash them afterward?
They should be in your All Mail folder. Use the WebUI and search for them and trash them, or select the "[Gmail]\All Mail" folder (watch out for localization, this can change name for non-English users).
I want to make a while loop that checks if a certain error message is printed on the screen. I check to see if a current name is available to take and if it isn't I do something and if it is I do something different.
I want it to say something like while the screen says ''Username has already been taken'' it will do one thing and if it does not come up on the screen it just skips the while loop.
When the error message is triggered:
<p id="username_notification" class="notification" style="">Username has already been taken</p>
Also the xpath of it in case it is needed:
//*[#id="username_notification"]
Also probably not needed but here is the message if the username is okay to use:
<p id="username_notification" class="notification" style="">Available!
</p>
And xpath for that:
//*[#id="username_notification"]
Its also probably obvious but I'm new to python and coding in general. Thanks!
I assume your script is entering a username into a web site that either checks availability passively, or requires the username to be actively submitted by your script to be checked. Either way, I assume your script is managing that requirement.
I assume you want a while loop to test until either one of the <p> elements with id="username_notification" to manage any lag between the script entering a username and the server response when availability is checked.
I assume the <p> element is going to be inserted into the DOM at a predictable location and will always have the id="username_notification".
So you want a while True loop which scrapes the page, checks for one or other of the <p> elements, when it finds one it breaks conditionally depending on which one it finds to discrete functions, e.g., on "Username has already been taken" re-enter a new username, or on "Available!" do something else...
But I'm not sure what your problem is in implementing it. What have you tried?
I'm writing a little Python program to automate the e-mail authentication process using selenium. Now, when I execute the program I always get the "email or password incorrect" message even if I'm sure that they are correct; I tried to execute the program line by line in the Python shell and I also changed the password to one without special characters just to be sure that selenium wasn't having problem with these specifically but without success so I think that somehow selenium is not "typing" the password correctly in the authentication form.
I'm using python 3.6; here's the code (without the real username and password for security reasons, of course).
from selenium import webdriver
browser = webdriver.Safari()
browser.get("myEmailProvider")
emailElem = browser.find_element_by_id("usernameDisplay")
emailElem.send_keys("myEmail")
passwordElem = browser.find_element_by_id("password")
passwordElem.send_keys("myEmailPassword")
passwordElem.submit()
Does anyone have any idea about why this is happening?
Thanks.
I had similar problem when I used Selenium to enter username and password. I have noticed that It would sometimes "eat" letter of two. For example, instead of typing "password" it would type "passord" or something like that.
I tried to find why this is happening, but without luck, however, I found workaround for this.
I put these things into while loop. So in your example I would do:
passwordElem = browser.find_element_by_id("password")
while(passwordElem != "myEmailPassword")
passwordElem.send_keys("myEmailPassword")
passwordElem.submit()code here
You can also put some limit how much times it can loop before It throws error, so you don't end up in infinite loop, but from my experience it send correct values in max 3 tries.
To work around this the best approach is to fill the form data in via javascript:
driver.execute_script('document.getElementById("usernameDisplay").value="myEmail"')
Here is a pastebin of the full script: http://pastebin.com/TfAc8sYM
Unfortunately there's no way for others to test this in my specific use-case as the API is not public.
There are a series of URLs that are http://example.com/api/users/get.ext?user=X
I want to go through these one at a time counting up and executing code for each URL.
If I load the API in the browser the XML looks just fine. Likewise if I print the requests.text data in the terminal everything is working correctly.
However when I run the script I get multiple outputs for the same API URL. I can see this in my database and printed via the command line. The amount of repeated lines per user entry seems consistent each time the script runs but it is inconsistent from user to user. The data within the repeated entries is identical.
Am I approaching the counting wrong?
Here's a sample of the XML. It's from the Vanilla Forums API:http://pastebin.com/aR51ShTM