繁体   English   中英

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

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

我使用Selenium-python抓取此页面 ,然后单击分页号。

我尝试这个:

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是因为有时如果不使用它就会出现此错误,并且我阅读了此问题并将其用于我的问题:

 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)

您应该尝试.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

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

cssSelec = 'ctl00_cphC_pn3T1_ctl01_rptPaginationH_ctl0%s_lbPage' %numbercssSelec = 'ctl00_cphC_pn3T1_ctl01_rptPaginationH_ctl%s_lbPage' %number 可以通过CSS选择器来引用ID本身,而不是使用选择器符号如下所示:

a#ctl00_cphC_pn3T1_ctl01_rptPaginationH_ctl02_lbPage

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

我强烈建议您避免使用Javascript单击元素,因为这充其量是一种hack,并且可能会在测试中产生Javascript错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM