简体   繁体   中英

How To Pick A Text From A List, Perform Certain Task With the Text Picked, Then Pick The Next Line Out Text In Selenium Python

I have a txt file and I want selenium to iterate through the txt file. It should Pick the First Line, perform certain action, then picks the second line, perform certain actions as well. It should keep doing that until it gets to the 10th line, then rest for 5 minutes. Pick the next 10, rest for 5 minutes, until all the lines are worked on.

But I am stuck on the first line, its picking the last line of the txt file only. Here is my code;

 # Picks the first Keyword file = open ('Keywords.txt', "r") lines = file.readlines() for line in lines: Keywordspace = driver.find_element_by_id('keyword') Keywordspace.send_keys(line.strip()) # Togls on Title ( action to be performed) checkBoxtitle = driver.find_element_by_xpath("//*[@id='create_article_form']/div[1]/div/div[4]/div[2]/div[2]/div/div/label") # Scroll to checkbox if its not in screen driver.execute_script("arguments[0].scrollIntoView();", checkBoxtitle) driver.execute_script("arguments[0].click();", checkBoxtitle) #Create With The First Line ( second action to be performed) create=driver.find_element_by_id('create_button') create.click() # Clear First Line driver.find_element_by_id('keyword').clear() #Adds Second Keyword for line in lines: Keywordspace = driver.find_element_by_id('keyword') Keywordspace.send_keys(line.strip())

This is what I was referring to. In this way, the all the code under the for loop would be executed for each iteration. Isn't it what you were looking for...

file = open ('Keywords.txt', "r")
lines = file.readlines()
for line in lines:
    Keywordspace = driver.find_element_by_id('keyword')
    Keywordspace.send_keys(line.strip())
    # Togls on Title ( action to be performed)

    checkBoxtitle = driver.find_element_by_xpath(
        "//*[@id='create_article_form']/div[1]/div/div[4]/div[2]/div[2]/div/div/label")
    # Scroll to checkbox if its not in screen
    driver.execute_script("arguments[0].scrollIntoView();", checkBoxtitle)
    driver.execute_script("arguments[0].click();", checkBoxtitle)

    # Create With The First Line ( second action to be performed)

    create = driver.find_element_by_id('create_button')
    create.click()

    # Clear First Line

    driver.find_element_by_id('keyword').clear()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM