0

I use Selenium-python to scrape this page and click on Pagination numbers.

i try this one:

driver = webdriver.Chrome()
time.sleep(5)
driver.get(
    'http://www.ooshop.com/courses-en-ligne/ContentNavigation.aspx?TO_NOEUD_IDMO=N000000013081&FROM_NOEUD_IDMO=N000000013056&TO_NOEUD_IDFO=81018&NOEUD_NIVEAU=2&UNIVERS_INDEX=2')
nbdespages = 23

for i in xrange(2,nbdespages):
    time.sleep(10)
    number = str(i)
    if(i<10):
        cssSelec = 'ctl00_cphC_pn3T1_ctl01_rptPaginationB_ctl0%s_lbPage' %number
    else:
        cssSelec = 'ctl00_cphC_pn3T1_ctl01_rptPaginationB_ctl%s_lbPage' %number
    try:
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        element = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CSS_SELECTOR, cssSelec)))
        driver.execute_script("arguments[0].click()", element)
        driver.find_element_by_css_selector(cssSelec).click()
    finally:
        driver.save_screenshot('/Users/Parik/Desktop/test.jpg')
        print "TRY Again, you will find"

but i got this exception:

TimeoutException: Message: 

i use WebDriverWait because sometimes i got this error if i don't use it and i read this question and use it for my problem:

 Element is not clickable at point 

the value of css selector is correct

POST http://127.0.0.1:51099/session/ddf78c5a19e720909cbe6a0f5408788e/element {"using": "css selector", "sessionId": "ddf78c5a19e720909cbe6a0f5408788e", "value": "ctl00_cphC_pn3T1_ctl01_rptPaginationB_ctl02_lbPage"}

UPADTE1

enter image description here

UPDATE 2

With Screen shot i see that i can't see the numbers, i changed my code to this:

    try:
        element = driver.find_element_by_css_selector('img#ctl00_cphC_pn3T1_ctl01_rp_ctl15_ctl00_iVisu.image')
        driver.execute_script("return arguments[0].scrollIntoView();", element)
        driver.execute_script("arguments[0].click()", element)
        driver.find_element_by_css_selector(cssSelec).click()
    finally:
        driver.save_screenshot('/Users/Parik/Desktop/test.jpg')
        print "TRY Again, you will find"

and now i see the Pagination enter image description here

but i have always this exception:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"ctl00_cphC_pn3T1_ctl01_rptPaginationB_ctl02_lbPage"}
  (Session info: chrome=51.0.2704.103)
  (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Mac OS X 10.11.5 x86_64)
Community
  • 1
  • 1
parik
  • 2,313
  • 12
  • 39
  • 67

2 Answers2

4

You should try .execute_script to perform click as below :-

try:driver.execute_script("document.getElementById(arguments[0]).click();", cssSelec)

You can also try as below :-

try:
element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, cssSelec))
)
driver.execute_script("arguments[0].click()", element)

Note :- you are preparing locator for id but using as By.CSS_SELECTOR to finding the element, for that reason you are getting exception as NoSuchElementException, so change it to By.ID.

You can also use element.click() but if you are getting Exception as like Element is not clickable at point, I suggest you can use here driver.execute_script("arguments[0].click()", element) to perform click.

Hope it will help you..:)

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
0

cssSelec = 'ctl00_cphC_pn3T1_ctl01_rptPaginationH_ctl0%s_lbPage' %number and cssSelec = 'ctl00_cphC_pn3T1_ctl01_rptPaginationH_ctl%s_lbPage' %number refer to the id's for the elements you're trying to click and are not applicable CSS selectors by themselves - you can use them in a CSS selector using the # symbol like so:

a#ctl00_cphC_pn3T1_ctl01_rptPaginationH_ctl02_lbPage

Another method you could use to locate and click the elements is to use WebDriver's link text locator as the elements are anchor (<a> tag) elements.

I would highly recommend you avoid using Javascript to click the elements as this a hack at best and opens up potential for Javascript errors in your tests.

Tom Trumper
  • 472
  • 2
  • 8
  • i added a# but i got this error : WebDriverException: Message: unknown error: Element is not clickable at point (151, 8). Other element would receive the click:
    ...
    (Session info: chrome=51.0.2704.103) (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Mac OS X 10.11.5 x86_64)
    – parik Jun 21 '16 at 10:38