我使用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。
希望它能对您有所帮助.. :)