2

I still have the same problem i deleted the code because i forgot to remove my password and so on.

Kliment
  • 29
  • 1
  • 4

3 Answers3

2

Collect each line from the file as a list of strings with

with open('file.txt') as f:
    lines = f.readlines()

then use a for to handle each string into variables or whatever you want to do with them

for l in lines:
    //handle line values here

You can also find your anwser in this other SO post: How to read a file line-by-line into a list?

Edited S Ahmed answer

from selenium import webdriver
browser = webdriver.Chrome('C:\\Users\klime\Desktop\Python Palai Bot\chromedriver')
browser.get('https://www.palai.org/u/sign_in') 
# "acc.txt" is your filepath/name
with open('acc.txt') as f:
    #this will put your each line in your text file in a list of strings, to use in the loop
    lines = f.readlines()
    #this will make a loop for each string in your list, using its value in a variable called "line".
    for line in lines:
        email = browser.find_element_by_id('user_email')
        email.send_keys(line)
        password = browser.find_element_by_id('user_password')
        password.send_keys('mypassword')
        commit = browser.find_element_by_name('commit')
        commit.click()
        collect = browser.find_element_by_link_text('Abholen')
        collect.click()
        collectTwo = browser.find_element_by_xpath('//*[@id=\"app\"]/article/div[1]/div/div/div/div[1]/div/div/form/input[1]')
        collectTwo.click()
        browser.get('https://palai.org/a/kliment+843fb7f2d-f17f-4f86-8365-0aeaf61f566e/pay?transfer_amount=176PALAI')
        submit = browser.find_element_by_id('submit_transfer')
        submit.click()
        logout = browser.find_element_xpath('//*[@id=\"app\"]/header/div[2]/form/button')
        logout.click()
        logout = browser.find_element_by_xpath('//*[@id=\"app\"]/header/div[2]/form/button')
        logout.click()
Valga
  • 459
  • 2
  • 7
  • I have trouble understanding your Answer. – Kliment Apr 25 '19 at 20:59
  • Like this? ```email = browser.find_element_by_id('user_email') with open('file.txt') as f: lines = f.readlines() for l in lines: exampleemail@mail.com mailexample@gmail.com mailexample2@gmail.com email.send_keys('myemail')``` – Kliment Apr 25 '19 at 21:00
  • for l in lines >>> if your text file has Line1 "text" Line2 "example" l will be for the first loop: "text" and the second "example" – Valga Apr 25 '19 at 21:03
  • Could you maybe Post the stuff you changed from @S Ahmed as a Answer? – Kliment Apr 26 '19 at 18:06
  • Could you please look at the main question i edited it i get a other error now – Kliment Apr 26 '19 at 20:45
  • There's another element in the front of the "commit" element you're trying to click. this is the element that is blocking the click: ... – Valga Apr 26 '19 at 20:51
2

You are almost there.

You have to log in and perform all the task you need to do and log out inside the loop where you are traversing the lines.

In the for loop you have to fix the indention of the send_keys statement. Python is space sensitive.

Also, you are passing the lines list in the send_keys which would through another error. Pass the l variable as it contains the item from the list.

Try This:

from selenium import webdriver
browser = webdriver.Chrome('C:\\Users\klime\Desktop\Python Palai Bot\chromedriver')


with open('acc.txt') as f:
    lines = f.readlines()

for l in lines:
    #if logout brings you to the login page then you could do this before the loop
    browser.get('https://www.palai.org/u/sign_in') 
    #here l is the object from the list. so use l instead of lines
    browser.find_element_by_id('user_email').send_keys(l)
    browser.find_element_by_id('user_password').send_keys('yourpassword')
    #Do your task
    #logout

-----------------

Edit: As I have said before python is space sensitive. You have to put indents to make your code to work.

Also, the readlines() returns the line with a newline. It causes your app to submit the form on send_keys the line. I've used the splitlines() to remove the newline. And put the code in a try block to handle any exception. With traceback you can handle the exception but still see the errors

Edit 2: You have to wait for the elements to appear before trying to access the element. That is why it was throwing NoSuchElement exception.
The brower.quit() closes the browser and destroys the webdriver instance. Otherwise it would still be running in the background. If you don't want your browser to close after running or on any exception, comment out it.

Try the following:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import traceback

browser = webdriver.Chrome(executable_path="D:\\DEV SOFT\\chromedriver_win32\\chromedriver.exe")
wait = WebDriverWait(browser, 30)
lines = open('acc.txt', 'r').read().split('\n')

for l in lines:
    try:

        browser.get('https://www.palai.org/u/sign_in')
        email = wait.until(EC.visibility_of(browser.find_element_by_id("user_email")))
        email.send_keys(l)
        # browser.find_element_by_id('user_email').send_keys(l)
        password = wait.until(EC.visibility_of_element_located((By.ID,"user_password")))
        password.send_keys("Hard49Pierburg49")

        commit = wait.until(EC.visibility_of(browser.find_element(By.NAME,'commit')))
        commit.click()

        collect = wait.until(EC.visibility_of(browser.find_element_by_link_text('Abholen')))
        collect.click()

        collectTwo = wait.until(EC.visibility_of(browser.find_element_by_xpath('//*[@id=\"app\"]/article/div[1]/div/div/div/div[1]/div/div/form/input[1]')))
        collectTwo.click()

        browser.get('https://palai.org/a/kliment+843fb7f2d-f17f-4f86-8365-0aeaf61f566e/pay?transfer_amount=176PALAI')

        submit = wait.until(EC.visibility_of(browser.find_element_by_id('submit_transfer')))
        submit.click()

        logout = wait.until(EC.visibility_of(browser.find_element_by_css_selector("form.button_to > button")))
        logout.click()
        # browser.save_screenshot("palai.png")
    except Exception as e:
        traceback.print_exc()
        browser.quit()

browser.quit()
S Ahmed
  • 1,454
  • 1
  • 8
  • 14
  • 1
    Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/192492/discussion-on-answer-by-s-ahmed-how-do-you-read-a-txt-file-line-by-line-and-use). – Bhargav Rao Apr 27 '19 at 19:52
0

To read/write to a file, you must first open it using file = open('fileName.txt','x') where x can be either a for append, w for write, r for read, and r+ for read and write. If you use write, it will delete any contents on file before writing. Append will, well, append it to the end of file. To read a line, you just use the function line = file.readline() in a while loop, breaking when line == null. To write to a file, you just go file.write("Blahblahblah \n'). Note the \n is only necessary to create new lines. Otherwise it would just keep writing at the end of the last character.

For more info just look at the docs, they're fairly basic:

https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files