使用文本文件在selenium中交替或逐个搜索

2024-05-29 04:57:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法获得如何使用我的数据库中的名称列表交替搜索的算法txt文件. 忍受我的英语我希望我能解释得更好。 我的txt文件包含:

Busia Retail Supervisor Demo
Busia Retail Supervisor Demo
Butere RS Demo Account
Butere RS Demo Account
Byangabo RS Demo Account
Byangabo RS Demo Account
Dapaong RS Demo
Dawanau RS Demo
Delveaux RS Demo Account

我的代码是:

from selenium import webdriver
acc = open(r'C:\Users\IanJayloG\Desktop\Python Files\Ex_Files_Learning_Python\Exercise Files\Test\test3py.txt')
myacc = acc.read()

driver=webdriver.Chrome(executable_path=r"C:\Users\IanJayloG\Desktop\Python Files\Ex_Files_Learning_Python\Exercise Files\Test\Driver\chromedriver.exe")
driver.get("https://ump.bboxx.co.uk/#/search")

for i in myacc:
    driver.find_element_by_name("filterText").send_keys(myacc) #Search tab
    driver.find_element_by_xpath("/html/body/app-root/app-main-view/main/app-get/div[2]/table/tbody/tr/td[14]/div/button[2]/i").click() #submit button

我想要的输出是,在单击submit之后,能够交替地逐个搜索文件中的名称列表。 很抱歉问你们这个问题,我已经用我自己的主动权在过去的几天里,我只是不能得到它。你知道吗

我得到的结果是无限的Busia Retail Supervisor DemoBusia Retail Supervisor DemoButere RS Demo AccountButere RS Demo AccountByangabo RS Demo AccountByangabo RS Demo AccountDapaong RS DemoDawanau RS DemoDelveaux RS Demto AccountDivo RS Demo accounBusia Retail Supervisor DemoBusia Retail Supervisor DemoButere RS Demo AccountButere RS Demo AccountByangabo RS Demo AccountByangabo RS Demo AccountDapaong RS DemoDawanau RS DemoDelveaux RS Demo AccountDivo RS Demo account

ERROR


Tags: 文件txt名称app列表demodriveraccount
1条回答
网友
1楼 · 发布于 2024-05-29 04:57:26

第一个问题是,当使用acc.read()时,您可以在一个字符串中获得整个文本。所以循环对每个字母进行迭代。(添加print(i)来检查)。你知道吗

最好使用acc.readlines(),文件的每一行都将添加到一个列表中。你知道吗

在循环中发送新的文本序列之前,只需clear输入字段,但是send_keys只需要一行,而不需要整个myacc

myacc = acc.readlines()

driver=webdriver.Chrome(executable_path=r"C:\Users\IanJayloG\Desktop\Python Files\Ex_Files_Learning_Python\Exercise Files\Test\Driver\chromedriver.exe")
driver.get("https://ump.bboxx.co.uk/#/search")

for words in myacc:
    driver.find_element_by_name("filterText").clear()
    driver.find_element_by_name("filterText").send_keys(words)
    driver.find_element_by_xpath("/html/body/app-root/app-main-view/main/app-get/div[2]/table/tbody/tr/td[14]/div/button[2]/i").click()

注意,如果在每次查询之后字段已经重置,那么方法clear()可能是可选的。你知道吗

以下是相关文档:https://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webelement.WebElement.clear

相关问题 更多 >

    热门问题