dimanche 18 février 2018

Selenium with Python Automation Script nested if loop not working

I am trying to run a script that will go through a page and sequentially click on every instance there is a giveaway. Some giveaway pages have two check boxes, and some have one. I need to be able to have the ability to pass through a page where there is either one or two.

Furthermore, I need this to be able to loop as many times as needed on each page and then proceed onto the next page of giveaways (there are many pages). I did not include this part in the code yet. I am not sure how to determine how many pages to click through and then repeat the first part (entering each giveaway).

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

#start new browser session
browser = webdriver.Chrome(executable_path='O:\Selenium\chromedriver.exe')

browser.set_page_load_timeout(20)

#navigating to webpage
browser.get('https://www.example.com')

#sign in
browser.find_element_by_link_text('Sign In').click()
browser.find_element_by_name("user[email]").send_keys('user@email.com')
browser.find_element_by_name("user[password]").send_keys('password')
browser.find_element_by_name('next').click()

#enter giveaways on page
enter = browser.find_elements_by_link_text('Enter Giveaway')
for x in range(0,len(enter)):
if enter[x].is_displayed():
    enter[x].click()
    #click on giveaway criteria etc
    browser.find_element_by_link_text('Select This Address').click()
    browser.find_element_by_name("entry_terms").click()
    browser.find_element_by_name("want_to_read").click()
    browser.find_element_by_name("commit").click()
    #start back at giveaway list
    browser.get('https://www.example.com')

I also had played around with some other for/if options that didn't work:

enter = browser.find_elements_by_link_text('Enter Giveaway')
for x in range(0,len(enter)):
if enter[x].is_displayed():
    enter[x].click()
    browser.find_element_by_link_text('Select This Address').click()
    browser.find_element_by_name("entry_terms").click()
    wtr = browser.find_element_by_name("want_to_read")
    if wtr.is_displayed():
        browser.find_element_by_name("want_to_read").click()
        else:
            browser.find_element_by_name("commit").click()        
    browser.find_element_by_name("commit").click()
    browser.get('https://www.example.com')
    browser.implicitly_wait(5)

Do I need to use 'while' instead? Or do I need to do an 'assert' for this? I am not sure where to proceed. I have looked at many other questions on Stack Overflow but none seem to help.

Here is the output (I am not sure if I have other errors for each version of the script but this is the most recent?):

Traceback (most recent call last): File "o:\PycharmProjects\pytest\testTwo_nestedIf.py", line 27, in browser.find_element_by_name("want_to_read").click() File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 487, in find_element_by_name return self.find_element(by=By.NAME, value=name) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 955, in find_element 'value': value})['value'] File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute self.error_handler.check_response(response) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"want_to_read"}

Aucun commentaire:

Enregistrer un commentaire