2017-08-03 106 views
0

我正在使用硒(python)網頁抓取。代碼中有很長的一段。所以,我正在使用循環。當我單獨運行代碼行時,它工作正常,但是當我使用循環時,它不工作。這裏有兩個錯誤:點擊不工作硒在網頁抓取使用python

WebDriverException: Message: unknown error: Element is not clickable at point (862, 13). Other element would receive the click: <div class="skycom_container">...</div> 
(Session info: chrome=46.0.2490.80) 
(Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 6.1 SP1 x86_64) 

WebDriverException: Message: unknown error: Element is not clickable at point (924, 786). Other element would receive the click: <div id="silentUIblocker" style="display: block;"></div> 
(Session info: chrome=46.0.2490.80) 
(Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 6.1 SP1 x86_64) 

這些常見或特定的錯誤? 這是在使用click()語句之前發生的。

這裏是我的代碼:

from selenium import webdriver 
import time 
driver = webdriver.Chrome('C:\Users\name\Downloads\chromedriver_win32 (3)\chromedriver.exe') 


driver.get('https://www.sky.com/shop/beta?s_tnt=87085:31:0') 
driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div/div[5]/article/button/div[1]/div[2]/div/h2').click() 
driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div/div[6]/section/div/div/div/div/div[1]/article/a').click() 
driver.find_element_by_xpath('//*[@id="polaris"]/div/div/div/section/div/div[2]/a[2]').click() 
driver.find_element_by_xpath('//*[@id="dsl-postcode"]').send_keys("E11 2LX") 
driver.find_element_by_xpath('//*[@id="dsl-check-landline"]').click() 
driver.find_element_by_xpath('//*[@id="dsl-addresses"]/option[2]').click() 
driver.find_element_by_xpath('//*[@id="dsl-multiple-address-select"]').click() 
driver.find_element_by_xpath('//*[@id="dsl-numberPortingNo"]').click() 
driver.find_element_by_xpath('//*[@id="dsl-number-porting-submit"]').click() 
driver.find_element_by_xpath('//*[@id="summaryBackLink"]').click() 
driver.find_element_by_xpath('//*[@id="oneOffCostToolTip"]').click() 
bb_pack = ["SKY_FIBRE_CAPPED", "BB_MAX"] 
for i in bb_pack: 
    driver.find_element_by_xpath('//*[@id="productButtonControls_%s"]/label' % i).click() 
    bb_name1.append(driver.find_element_by_xpath('//*[@id="productButtonControls_%s"]/label' % i).text) 
    pack = ["ANYTIME_EXTRA", "INTERNATIONAL_EXTRA"] 
    for j in pack: 
    driver.find_element_by_xpath('//*[@id="productButtonControls_ST_%s"]/label' % j).click() 
    bb_name2.append(driver.find_element_by_xpath('//*[@id="productButtonControls_ST_%s"]/label' % j).text) 
    #more details in this loop 
+0

考慮將你的'Chrome'升級到60.0&'chromedriver'爲2.30。謝謝 – DebanjanB

+0

[Selenium Web Driver&Java可能重複。元素在點(36,72)處不可點擊。其他元素會收到點擊:](https://stackoverflow.com/questions/44912203/selenium-web-driver-java-element-is-not-clickable-at-point-36-72-other-el) – DebanjanB

+0

這通常發生在Dom進行更新並且webdriver嘗試點擊或另一個隱藏元素阻止了您的元素時。嘗試在點擊之間等待/休息。 –

回答

1

用流利的等待下文。它會等到你的元素沒有準備好頁面: -

WebElement waitsss(WebDriver driver, By elementIdentifier){ 
    Wait<WebDriver> wait = 
       new FluentWait<WebDriver>(driver).withTimeout(60, TimeUnit.SECONDS) 
               .pollingEvery(1, TimeUnit.SECONDS) 
              .ignoring(NoSuchElementException.class); 

return wait.until(new Function<WebDriver, WebElement>() 
     { 
      public WebElement apply(WebDriver driver) { 
        return driver.findElement(elementIdentifier); 
      } 
      }); 
} 

等待應該適合你。如果問題仍然存在,則使用JavascriptExecutor。它將通過JS直接操作。它應該工作。我給一個例子,點擊使用JavascriptExecutor

WebElement element = driver.findElement(By.id("gbqfd")); 
JavascriptExecutor executor = (JavascriptExecutor)driver; 
executor.executeScript("arguments[0].click();", element); 

我剛纔看見你正在使用python的任何元素,在蟒蛇,它應該是象下面這樣: -

driver.execute_script("arguments[0].click();", element) 

腳本應該像下面: -

driver.execute_script("document.getElementsByClassName('skycom_container')[0].click()") 
driver.execute_script("document.getElementById('silentUIblocker').click()") 

最終代碼

driver = webdriver.Chrome('C:\Users\name\Downloads\chromedriver_win32 (3)\chromedriver.exe') 
    driver.implicitly_wait(30) # seconds 
    driver.get('https://www.sky.com/shop/beta?s_tnt=87085:31:0') 
    driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div/div[5]/article/button/div[1]/div[2]/div/h2').click() 
    element=driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div/div[6]/section/div/div/div/div/div[1]/article/a') 
    driver.execute_script("arguments[0].click();", element) 
    driver.find_element_by_xpath('//*[@id="polaris"]/div/div/div/section/div/div[2]/a[2]').click() 
    driver.find_element_by_xpath('//*[@id="dsl-postcode"]').send_keys("E11 2LX") 
    driver.find_element_by_xpath('//*[@id="dsl-check-landline"]').click() 
    driver.find_element_by_xpath('//*[@id="dsl-addresses"]/option[2]').click() 
    driver.find_element_by_xpath('//*[@id="dsl-multiple-address-select"]').click() 
    driver.find_element_by_xpath('//*[@id="dsl-numberPortingNo"]').click() 
    driver.find_element_by_xpath('//*[@id="dsl-number-porting-submit"]').click() 
    driver.find_element_by_xpath('//*[@id="summaryBackLink"]').click() 
    driver.find_element_by_xpath('//*[@id="oneOffCostToolTip"]').click() 

希望它會幫助你:)

+0

你能告訴我python的確切代碼行嗎? Thanx提前.. :) –

+0

按照下面的鏈接,如果你有類名或ID,你可以直接點擊..參考: - https://stackoverflow.com/questions/36987006/how-to-click-a-javascript-按鈕與硒 –

+0

是否有可能通過我的代碼,並幫助我? –

1

嘗試只是讓click()前添加以下的進口

from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 

,插入下面一行:

WebDriverWait(driver, 10).until(EC.invisibility_of_element_located((By.ID, "silentUIblocker"))) 

這應該讓你等到元素攔截點擊消失

+0

是的,它的工作原理。我剛剛添加了另一個錯誤。你能看到嗎? –

+0

嘗試用相同的方式解決:'WebDriverWait(driver,10).until(EC.invisibility_of_element_located((By.CLASS_NAME,「skycom_container」)))' – Andersson

+0

仍然沒有完成...得到這個錯誤: C:\ Anaconda2 (),> end_time: 79 break ---> 80 raise TimeoutException(message, screen,stacktrace) 82 def until_not(self,method,message ='): TimeoutException:消息: –