Read dictionary key with Selenium Python

1

I have the following code. The program is complete, only I need a small adjustment. As you can see, I read a dictionary and I'm entering the keys in different places on the selenium website. Now, here the problem appears, I need you to just call me the keys that are inside the ID, not all of the dictionary. To be more clear, when I execute my function, {'829690':['testmartin','--test$2--'], these data belong to the first action of the program. the 829690 is entered in my URL and calls it. Then the data that is within 829690 sends with nombre.send_keys(*value) . There's the problem, I also load the '1016244' .

Results when executing the program:

"https://onevideo.aol.com/#/marketplaceconnection/829690"
valores llamados: 'testmartin','--test$2--','camionetas','autos'
"https://onevideo.aol.com/#/marketplaceconnection/1016244"
valores llamados: 'testmartin','--test$2--','camionetas','autos'

Results I need:

"https://onevideo.aol.com/#/marketplaceconnection/829690"
valores llamados: 'testmartin','--test$2--'
"https://onevideo.aol.com/#/marketplaceconnection/1016244"
valores llamados: 'camionetas','autos'

I think I was clear with what I'm looking for. Any doubt consult. I leave the complete code in the link: link

Lista = {'829690':['testmartin','--test$2--'], 
          '1016244':['camionetas','autos']}

for lista in Lista.itervalues():
            for value in lista:
                nombre.send_keys(*value)
    
asked by Martin Bouhier 19.10.2017 в 16:59
source

2 answers

2

Use dict.iteritems , first use the key to create the url, then iterate over the tags of that url and thus for each dictionary key:

Lista = {'829690' :['testmartin','--test$2--'], 
         '1016244':['camionetas','autos']}

for id, tags in Lista.iteritems():
    url = driver.get("https://onevideo.aol.com/#/marketplaceconnection/{}".format(id.strip()))
    # Codigo a ejecutar antes de pasar los tags

    for tag in tags:
        nombre.send_keys(tag)

If I understand you, you should do something like this:

def Target():
    for id,  tags in Lista.iteritems():
        url = driver.get("https://onevideo.aol.com/#/marketplaceconnection/{}".format(id.strip()))
        try:
            element_present = EC.presence_of_element_located((By.CSS_SELECTOR, ".navbar-inhiner > ul:nth-child(1) > li:nth-child(4) > a:nth-child(1)"))
            WebDriverWait(driver, timeout).until(element_present)

        except TimeoutException:
            print ("Timed out waiting for page to load")
        driver.find_element_by_css_selector('.navbar-inhiner > ul:nth-child(1) > li:nth-child(4) > a:nth-child(1)').click()
        try:
            element_present = EC.presence_of_element_located((By.CSS_SELECTOR, "li.ng-scope:nth-child(6) > a:nth-child(1)"))
            WebDriverWait(driver, timeout).until(element_present)

        except TimeoutException:
            print ("Timed out waiting for page to load")    
        driver.find_element_by_css_selector('li.ng-scope:nth-child(6) > a:nth-child(1)').click()
        #Cargado de tags
        try:
            element_present = EC.presence_of_element_located((By.CSS_SELECTOR, "span.ng-scope:nth-child(12) > div:nth-child(1) > span:nth-child(1)"))
            WebDriverWait(driver, timeout).until(element_present)

        except TimeoutException:
            print ("Timed out waiting for page to load")
        driver.find_element_by_css_selector('span.ng-scope:nth-child(12) > div:nth-child(1) > span:nth-child(1)').click()
        try:
            element_present = EC.presence_of_element_located((By.CSS_SELECTOR, "#s2id_autogen30"))
            WebDriverWait(driver, timeout).until(element_present)

        except TimeoutException:
            print ("Timed out waiting for page to load")    
        nombre = driver.find_element_by_css_selector('#s2id_autogen30')

        for tag in tags:
            nombre.send_keys(tag)
            try:
                element_present = EC.presence_of_element_located((By.CSS_SELECTOR, '.select2-results-dept-0'))
                WebDriverWait(driver, timeout).until(element_present)
            except TimeoutException:
                print ("Timed out waiting for page to load")
            nombre2 = driver.find_element_by_css_selector('.select2-results-dept-0').click()
        driver.find_element_by_css_selector('button.btn-success:nth-child(3)').click()
        try:
            element_present = EC.presence_of_element_located((By.CSS_SELECTOR, "div.modal-footer:nth-child(3) > button:nth-child(1)"))
            WebDriverWait(driver, timeout).until(element_present)

        except TimeoutException:
            print ("Timed out waiting for page to load")
        driver.find_element_by_css_selector('div.modal-footer:nth-child(3) > button:nth-child(1)').click()
        #Guardado de la connection

        try:
            element_present = EC.presence_of_element_located((By.CSS_SELECTOR, ".bs-docs-social-buttons > li:nth-child(2) > button:nth-child(1)"))
            WebDriverWait(driver, timeout).until(element_present)
        except TimeoutException:
            print ("Timed out waiting for page to load")
        driver.find_element_by_css_selector(".bs-docs-social-buttons > li:nth-child(2) > button:nth-child(1)").click()
        element_present = EC.presence_of_element_located((By.CSS_SELECTOR, "adap-marketplace-connections-grid-filter.ng-scope > button:nth-child(1)"))
        WebDriverWait(driver, timeout).until(element_present)

Not being able to access the website, I do not know if everything is indented where it should be, but the idea should be this one.

You should not use classes, your application follows a structured paradigm, not object-oriented programming. It does not make sense to create an object that is not really such, just execute code. Use functions ( def ) and call them in the main as you do with the classes.

    
answered by 19.10.2017 / 17:27
source
1

I do not understand the problem. Your code works:

    
answered by 19.10.2017 в 17:27