我試圖將多個條目提交到登錄名/密碼后面的網路表單中。資料來自 CSV。
在 Selenium (python) 中,當我的代碼完成時,它會回圈回傳,打開一個全新的 Chrome 實體,并使用新的組態檔,然后必須再次登錄,然后才能輸入 CSV 的下一行。
我不想將密碼存盤在 python 檔案/CSV 中,也不想通過在一分鐘內登錄 10-20 次來創建看起來很奇怪的流量。
有什么建議的代碼可以在同一 Chrome 實體中打開一個新視窗/選項卡,并在不啟動新實體的情況下將資料條目移動到 CSV 中的下一行?
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])
uj5u.com熱心網友回復:
您在回圈中呼叫 web = webdriver.Chrome() ,這就是它重復多次的原因。
檢查此代碼:
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])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/453182.html
