【问题标题】:Click not working for selenium in web scraping using python在使用 python 进行网页抓取时单击不适用于 selenium
【发布时间】:2017-08-03 08:55:01
【问题描述】:

我正在使用 selenium(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

【问题讨论】:

标签: python selenium web-scraping automation


【解决方案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,在 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()

希望对你有帮助:)

【讨论】:

  • 你能告诉我python的确切代码行吗?提前谢谢..:)
  • 如果你有classname或者id可以直接点击下面的链接..参考:-stackoverflow.com/questions/36987006/…
  • 您可以浏览我的代码并帮助我吗?
  • 你能告诉我导致这个错误的元素的 id 或类吗?确保 id 或 class 在 DOM 上必须是唯一的
  • ...
    。我从生成的错误中得到了这个。
【解决方案2】:

尝试在下面添加导入

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

并在创建click()之前插入以下行:

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

这应该允许你等到拦截点击的元素消失

【讨论】:

  • 是的,它有效。我刚刚添加了另一个错误。你也看到了吗?
  • 尝试用同样的方法解决:WebDriverWait(driver, 10).until(EC.invisibility_of_element_located((By.CLASS_NAME, "skycom_container")))
  • 仍然没有完成..得到这个错误:C:\Anaconda2\lib\site-packages\selenium\webdriver\support\wait.pyc in until(self, method, message) 78 if time. time() > end_time: 79 break ---> 80 raise TimeoutException(message, screen, stacktrace) 81 82 def until_not(self, method, message=''): TimeoutException: Message:
  • 甚至 WebdriverWait 也停止工作了!为什么会出现这种情况!!?我真的需要帮助..:(
  • 你能分享页面 URL 和你想要做什么的描述吗?
猜你喜欢
  • 1970-01-01
  • 2021-05-08
  • 2018-07-20
  • 2020-03-13
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多