0
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()

I want to add the second value from my csv file, how I can append second value from my csv file after it makes one.

3
  • your question is not clear Commented Feb 12, 2021 at 16:56
  • what exactly are you trying to archive? add to you csv file or read from it and add its data to your browser? Commented Feb 12, 2021 at 17:37
  • Yes I'm trying to read from csv file and add data into my browser, but it only adds first row and not others Commented Feb 12, 2021 at 17:57

1 Answer 1

0

You close driver as the final step of iteration so that when your loop goes to the second iteration, your browser is closed.

Try to change this:

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()

to this:

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.