2

I am not sure how to simulate clicking the "Next", my current code will just stop at first instance..I also created a for loop but it would just say object is not iterable..any way to revise my code?

from selenium import webdriver
browser = webdriver.Firefox()
import time
browser.get('https://www.autocodes.com/obd-code-list/powertrain/1')

linkElem = browser.find_element_by_css_selector("#pag > a")
type(linkElem)
linkElem.click() # follows the "Next" link
  • whats with yield? http://stackoverflow.com/questions/231767/what-is-the-function-of-the-yield-keyword – Ari Gold Jan 02 '17 at 10:49
  • sorry forgot to tell I am just starting python... – Curious Cat Jan 02 '17 at 10:51
  • no prob, check the yield section in the example and try it with a simple sandbox case to understand – Ari Gold Jan 02 '17 at 10:52
  • okay but hopefully somebody has a code or something, I learn fast by demo, not really spoon-feed because when somebody show me a code I will revise it and experiment on it – Curious Cat Jan 02 '17 at 10:57
  • you can get all links `find_elements_by_css_selector("#pag a")` and use only `linkElem[-2]` – furas Jan 02 '17 at 11:07

2 Answers2

1

try the following code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


browser = webdriver.Firefox()
import time
count = 0
browser.get('https://www.autocodes.com/obd-code-list/powertrain/1')
while(1):
    try:
        linkElem = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@id='pag']//a[contains(text(),'Next')]")))
    except:
        import traceback
        traceback.print_exc()
        print "last page reached."
        break;
    # type(linkElem)
    count += 1
    print "count " , count
    linkElem.click() # follows the "Next" link
    time.sleep(1)

the code continuously checks whether the Next element is present in an infinite while loop. If present, clicks it. Otherwise, break the loop.

Note: count variable is added as a debug statement to know the loop count. If not required, you can remove the code related to it.

Note: similarly, traceback is added to print the complete trace. just prints the exception trace for your reference.

Note: break keyword, breaks the infinite loop once it reaches the last page and comes out of while loop and continues with the next code.

References:

  1. http://selenium-python.readthedocs.io/waits.html
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
  • this code is perfect!!! I already saw this kind of coding but due to my limited programming knowledge, I am discourage to tinker on it..many thanks..I will study this code piece by piece.. – Curious Cat Jan 02 '17 at 11:09
  • Added notes for better understanding of the logic. I hope it helps you. – Naveen Kumar R B Jan 02 '17 at 11:11
  • you can wish me on this new year by providing some reputation, by accepting the answer :) (only if it helps you). that's the best way to say thanks in SO! Welcome to SO! – Naveen Kumar R B Jan 02 '17 at 11:16
  • dumb statement but I'm also new to stackoverflow, I already upvoted your answer..it would help if you guide me about this reputation thing.. – Curious Cat Jan 02 '17 at 11:20
  • You can't upvote till you reach `25 or 20` rep (not sure). Still, you can accept the answer for your own question (tick mark next to my answer) – Naveen Kumar R B Jan 02 '17 at 11:21
  • ok I googled it and found the same as your comment..already accepted your answer..I was pain in the ass.. – Curious Cat Jan 02 '17 at 11:22
  • The sleep at the end is unnecessary since you are using an explicit wait anyway, and arbitrarily slows down the code execution. – Levi Noecker Jan 02 '17 at 23:29
  • @levinoecker, I tried that first, but infinite loop is executing by clicking on the same Next button in the first page each time. So, added sleep of 1 second. If it is working without sleep for OP, he can remove the sleep. Thanks for the suggestion. – Naveen Kumar R B Jan 03 '17 at 04:34
0

May be your element id or type of "linkElem" get changed after first click.

Nilesh Awari
  • 104
  • 4
  • not sure, but when I start on getting css_selector in page "powertrain/1"..it says #pag > a:nth-child(1) but then on "....powertrain/2" it will say #pag > a:nth-child(3) up to the last page..could this be the reason? how could I fix it? – Curious Cat Jan 02 '17 at 11:00
  • Yes! that's the problem. after first click. element location get changed so that why you unbale to iterate. – Nilesh Awari Jan 02 '17 at 11:07
  • thanks for the info, I was just starting in programming they say python is relatively essay, so I pick it.. – Curious Cat Jan 02 '17 at 11:16