I attempting to submit multiple entries into a webform behind a login/password. The data is from a CSV.
In Selenium (python), when my code finishes it loops back, opens an entirely new Chrome instance with a fresh profile, and then has to log in again before entering the next row of the CSV.
I dont want to store my password in the python file/CSV nor do I want to create weird looking traffic by logging in 10-20 times in a minute.
Any suggested code to open a new window/tab within the same instance of Chrome and to have the data entry move to the next row in the CSV without starting a new instance?
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
import csv
PATH = "/usr/local/bin/chromedriver"
url = "https://123.com"
with open('test.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
for line in csv_reader:
web = webdriver.Chrome()
web.get(url)
time.sleep(1)
username = web.find_element(By.XPATH,"/html/body/app-root/div/app-login/mat-card/mat-card-content/form/div[1]/mat-form-field/div/div[1]/div/input")
username.send_keys('[email protected]')
companyaddressnum = web.find_element(By.XPATH,"/html/body/app-root/div/form/div[2]/mat-card[2]/mat-card-content/div[3]/mat-form-field[1]/div/div[1]/div/input")
companyaddressnum.send_keys(line[2])
CodePudding user response:
You're calling web = webdriver.Chrome() in the loop, that's why it's repeating multiple times.
Check this code:
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
import csv
PATH = "/usr/local/bin/chromedriver"
url = "https://123.com"
web = webdriver.Chrome()
with open('test.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
for line in csv_reader:
web.get(url)
time.sleep(1)
username = web.find_element(By.XPATH,
"/html/body/app-root/div/app-login/mat-card/mat-card-content/form/div[1]/mat-form-field/div/div[1]/div/input")
username.send_keys('[email protected]')
companyaddressnum = web.find_element(By.XPATH,
"/html/body/app-root/div/form/div[2]/mat-card[2]/mat-card-content/div[3]/mat-form-field[1]/div/div[1]/div/input")
companyaddressnum.send_keys(line[2])