Python Forum
Issue with If statement inside for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with If statement inside for loop
#1
Hey guys!

I've been working on this script for a few days now. What it's supposed to do is go to a real estate listing page click on a listing, then click on the realtors personal website, and print out the URL THEN closing the window and returning back to the beginning of the loop. I ran in to an issue when the script came across a listing where the agent had no website which resulted in an error. I then tried using a try except which worked until the script hit the listing without an agent website. It would skip the listing and go back to the home page and close the whole browser. I got a solution off stackoverflow but now I get this issue:

Traceback (most recent call last):
File "/home/yxty/Realex.py", line 16, in <module>
WebDriverWait(driver, 15).until(lambda d: len(driver.window_handles) == 2)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

Here is my code:

time.sleep(15)
for i in range(1,9):
    listing_page = driver.find_element_by_xpath('//*[@id="m_property_lst_cnt_realtor_more_'+str(i)+'"]').click()
    realtor_url = driver.find_elements_by_xpath('//*[@id="lblMediaLinks"]/a')
    if (len(realtor_url)) > 0:
        realtor_url[0].click
        WebDriverWait(driver, 15).until(lambda d: len(driver.window_handles) == 2)
        driver.switch_to_window(driver.window_handles[1])
        driver.close()
        driver.switch_to_window(driver.window_handles[0])
    driver.get(home_page)
    time.sleep(10)
Reply
#2
Line 6, you never click the link.  You just reference the click function (which does nothing if you don't use the reference).

But that's unrelated.  Selenium is timing out.  What's the rest of the error message?
Reply
#3
(Oct-12-2017, 04:33 PM)nilamo Wrote: Line 6, you never click the link.  You just reference the click function (which does nothing if you don't use the reference).

But that's unrelated.  Selenium is timing out.  What's the rest of the error message?

That's what I was thinking, here is the entire error message:

Also, how would you properly reference the .click() function?

Error:
Traceback (most recent call last): File "/home/yxty/Realex.py", line 16, in <module> WebDriverWait(driver, 15).until(lambda d: len(driver.window_handles) == 2) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/support/wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: [Finished in 47.1s with exit code 1] [shell_cmd: python -u "/home/yxty/Realex.py"] [dir: /home/yxty] [path: /home/yxty/bin:/home/yxty/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin]
Reply
#4
Quote:Also, how would you properly reference the .click() function?
You are already referencing it, but you're not doing anything with the reference. To call a function, use parentheses. ie: do this: something.click() instead of this: something.click
Reply
#5
(Oct-12-2017, 06:03 PM)nilamo Wrote:
Quote:Also, how would you properly reference the .click() function?
You are already referencing it, but you're not doing anything with the reference. To call a function, use parentheses. ie: do this: something.click() instead of this: something.click

Well there it is, that was the issue. I should have tried harder to figure that out myself, thank you very much!
Reply
#6
Thanks for letting us know!

Honestly, I'm surprised that fixed the issue.  But I've never used selenium yet, so idk lol
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020