我如何解决这个 python selenium 问题?

时间:2021-02-12 16:48:59

标签: python selenium

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
mainurl = 'https://austin.craigslist.org/search/cta?s=0'
driver.get(mainurl)

with open('C:/Users/Saba/Desktop/sample.csv', encoding="utf-8") as f:   
    for  i  in f:
        search = driver.find_element_by_xpath('//*[@id="query"]').send_keys(i)
        submit = driver.find_element_by_xpath('//*[@id="searchform"]/div[1]/button').click()
        driver.get(mainurl)
        driver.close()

我想从我的 csv 文件中添加第二个值,如何在我的 csv 文件中添加第二个值。

1 个答案:

答案 0 :(得分:0)

您在迭代的最后一步关闭驱动程序,这样当您的循环进入第二次迭代时,您的浏览器就会关闭。

尝试改变这一点:

path = "B:\\RogueSquadron\\python\\testFile.txt"
with open(path, 'r') as f:
    for word in f:
        print(word)

为此:

with open('C:/Users/Saba/Desktop/sample.csv', encoding="utf-8") as f:   
    for  i  in f:
        search = driver.find_element_by_xpath('//*[@id="query"]').send_keys(i)
        submit = driver.find_element_by_xpath('//*[@id="searchform"]/div[1]/button').click()
        driver.get(mainurl)
        driver.close()