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

Why Cant I Click an Element in Selenium using python and Chrome Webdriver?

我使用 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

更新 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"

现在我看到了分页

但我总是有这个例外:

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)

cssSelec = 'ctl00_cphC_pn3T1_ctl01_rptPaginationH_ctl0%s_lbPage' %numbercssSelec = 'ctl00_cphC_pn3T1_ctl01_rptPaginationH_ctl%s_lbPage' %number 请参考您尝试单击的元素的 ID,这些元素本身不适用 CSS 选择器 - 您 可以 在 [=26] 中使用它们=] 选择器使用 # 符号,如下所示:

a#ctl00_cphC_pn3T1_ctl01_rptPaginationH_ctl02_lbPage

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

我强烈建议您避免使用 Javascript 来单击元素,因为这充其量只是一种黑客攻击,并且可能会在您的测试中出现 Javascript 错误。

您应该尝试 .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 准备定位器,但使用 as By.CSS_SELECTOR 来查找元素,因此您会遇到异常 NoSuchElementException,所以改成By.ID.

你也可以使用 element.click() 但是如果你得到 Exception 就像 Element is not clickable at point,我建议你可以在这里使用 driver.execute_script("arguments[0].click()", element) 来执行 click .

希望对您有所帮助..:)