为什么我不能使用python和Chrome Webdriver点击Selenium中的元素?

时间:2016-06-21 07:39:05

标签: python selenium selenium-webdriver web-scraping selenium-chromedriver

我使用Selenium-python来抓取this page并点击分页编号。

我试试这个:

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"

但我得到了这个例外:

TimeoutException: Message: 

我使用 WebDriverWait ,因为如果我不使用它并且我阅读this question并将其用于我的问题,有时会出现此错误:

 Element is not clickable at point 

css选择器的值是正确的

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

更新2

使用屏幕截图我看到我无法看到数字,我将代码更改为:

    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"

现在我看到了分页 enter image description here

但我总是有这个例外:

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)

2 个答案:

答案 0 :(得分:2)

您应该尝试.execute_script执行点击操作,如下所示: -

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

您也可以尝试以下方法: -

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

注意: - 您正在为id准备定位器,但使用By.CSS_SELECTOR来查找元素,因此您将获得NoSuchElementException的异常,所以将其更改为By.ID

您也可以使用element.click(),但如果您Exception获得Element is not clickable at point,我建议您可以driver.execute_script("arguments[0].click()", element)使用click。< / p>

希望它会对你有所帮助.. :)

答案 1 :(得分:0)

cssSelec = 'ctl00_cphC_pn3T1_ctl01_rptPaginationH_ctl0%s_lbPage' %numbercssSelec = 'ctl00_cphC_pn3T1_ctl01_rptPaginationH_ctl%s_lbPage' %number指的是您尝试点击的元素的ID,并且它们本身不适用于CSS选择器 - 您可以使用符号如此:

a#ctl00_cphC_pn3T1_ctl01_rptPaginationH_ctl02_lbPage

您可以用来定位和单击元素的另一种方法是使用WebDriver的链接文本定位器,​​因为元素是锚(<a>标记)元素。

我强烈建议你避免使用Javascript来点击这些元素,因为这样做是最好的,并且在测试中可能会出现Javascript错误。