Repeatedly modify url based on a list and visit it

2

I have a code that I use to modify certain values of a URL. I explain the process, after login, I call another url in which I make changes. The problem is that I need to enter many URLs similar to these to do the same operation, the difference between the url is the ID (bold) link afebecf1-0bc3-4e1e-91ef-dc26de42aca7 / general. I want to enter the different IDs in the URL to be able to call all the URLs of the IDs

Those IDs I have in an Excel. What I'm looking for is to move them to a list (I think that's what's best for me) and to enter those ID values in the URL call to repeat the action I need. Capable with some function can be achieved.

import time
from selenium import webdriver
import csv

driver = webdriver.Chrome('/Users/Martin/Desktop/chromedriver')
driver.get("https://platform.io/en/login/")
user= driver.find_element_by_xpath('//*[@id="form_username"]')
user.send_keys("email")
passwd = driver.find_element_by_xpath('//*[@id="form_password"]')
passwd.send_keys("pass")
l_button = driver.find_element_by_xpath('//*[@id="login_submit"]')
l_button.click()
time.sleep(1)

driver.get("https://auto.io/en/users/inventory/afebecf1-0bc3-4e1e-91ef-dc26de42aca7/general")
time.sleep(1)
l_button = driver.find_element_by_xpath('//*[@id="inventory_general"]/fieldset/div[1]/div[2]/span/span[2]/span')
l_button.click()

Edit:

Trying to read the data of a CSV following the solution of the answer of FJSevilla I have found a new error. The csv has the following structure:

301d5e7b-a75f-4962-f38f-1e6e5db90dcc
f5b2b6ba-a4ce-4e17-b8f8-b8d912e4d6ec
7ed4aaa7-0b3f-44c6-f254-be986985942d
...

It's actually a single column as seen. The code with which I try to read it is:

import csv
with open("Ids para Pausar.csv", 'r') as my_file:
    reader = csv.reader(my_file, delimiter=',')
    my_list = list(reader)
    for id in my_list:
        url = "https://platform.io/en/users/inventory/{}/general".format(id.strip())
        driver.get(url)
        time.sleep(1)
        l_button = driver.find_element_by_xpath('//*[@id="inventory_general"]/fieldset/div[1]/div[2]/span/span[2]/span')
        l_button.click()
        l_button = driver.find_element_by_xpath('//*[@id="form_submit"]')
        l_button.click()
        time.sleep(2)

driver.close()

But I get the following error:

  

AttributeError: 'list' object has no attribute 'strip'.

    
asked by Martin Bouhier 03.10.2017 в 01:35
source

1 answer

2

The simplest idea is to load your column of ids with Pandas or any other specialized library from your Excel. Once you can iterate over ids you use for in and build each url using str.format :

lista = ['f17510fb-4678-4230-82d3-3be50fbecb6b',
         '6e296f1d-5edf-4d8e-‌​b601-9feb4dc6e177',
         '1‌​6d36513-c4ce-4128-be‌​c4-2c8cf60129f9']

for id in lista:
    url = "https://auto.io/en/users/inventory/{}/general".format(id.strip())

In your code it would be integrated like this at the beginning:

import time
from selenium import webdriver


driver = webdriver.Chrome('/Users/Martin/Desktop/chromedriver')
driver.get("https://auto.io/en/login/")
user= driver.find_element_by_xpath('//*[@id="form_username"]')
user.send_keys("email")
passwd = driver.find_element_by_xpath('//*[@id="form_password"]')
passwd.send_keys("pass")
l_button = driver.find_element_by_xpath('//*[@id="login_submit"]')
l_button.click()
time.sleep(1)

lista = ['f17510fb-4678-4230-82d3-3be50fbecb6b',
         '6e296f1d-5edf-4d8e-‌​b601-9feb4dc6e177',
         '1‌​6d36513-c4ce-4128-be‌​c4-2c8cf60129f9']

for id in lista:
    url = "https://auto.io/en/users/inventory/{}/general".format(id.strip())
    driver.get(url)
    time.sleep(1)
    l_button = driver.find_element_by_xpath('//*[@id="inventory_general"]/fieldset/div[1]/div[2]/span/span[2]/span')
    l_button.click()

If you have problems with the loading of the elements, look at the Explicit Waits.

Edit:

When you do my_list = list(reader) what you get is a list of lists where each internal list is a row:

[['301d5e7b-a75f-4962-f38f-1e6e5db90dcc'], ['f5b2b6ba-a4ce-4e17-b8f8-b8d912e4d6ec'], ...]

Since your csv only has one column, if it is always going to be that way, you do not need anything special to read it, iterate over the rows directly:

with open("Ids para Pausar.csv", 'r') as my_file:
    for id in my_file:
        url = "https://platform.io/en/users/inventory/{}/general".format(id.strip())

        # Resto del codigo

If I had more columns you can get the first column with something like:

import csv
with open("Ids para Pausar.csv", 'r') as my_file:
    reader = csv.reader(my_file, delimiter=',')
    my_list = (row[0] for row in reader) # 0 es el indice de la primera columna
    for id in my_list:
        url = "https://platform.io/en/users/inventory/{}/general".format(id.strip())
    
answered by 03.10.2017 / 11:19
source