【问题标题】:Not clickable at point - SELENIUM - PYTHON点不可点击 - SELENIUM - PYTHON
【发布时间】:2021-10-18 01:21:16
【问题描述】:

我正在尝试从https://www.xpi.com.br/investimentos/fundos-de-investimento/lista/#/ 展开如下箭头

我正在使用以下代码:

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep

url = 'https://www.xpi.com.br/investimentos/fundos-de-investimento/lista/#/'

driver = webdriver.Chrome(options=options)
driver.get(url)
sleep(1)


expandir = driver.find_elements_by_class_name("sly-row")[-4]
expandir.click()
sleep(4)

expandir_fundo = wait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class='arrow-details']")))
expandir_fundo.click()

我得到了错误:TimeoutException: Message

我也尝试使用下面的代码:

expandir_fundo = driver.find_element_by_xpath('//*[@id="investment-funds"]/div/div/div[2]/article/article/section[2]/div[1]/div[10]')
expandir_fundo.click()

并得到错误:ElementClickInterceptedException: Message: element click intercepted: Element ... is not clickable at point (1479, 8)。

在下面找到 HTML 的一部分:

<div class="funds-table-row sc-jAaTju kdqiDh sc-ckVGcZ drRPta">
  <div class="fund-name sc-jKJlTe hZlCDP" title="Bahia AM Maraú Advisory FIC de FIM" style="cursor:pointer;"><div>Bahia AM Maraú Advisory FIC de F...</div><p class="sc-brqgnPfbcFSC">Multimercado</p></div>
  <div class="morningstar sc-jKJlTe hZlCDP">-</div>
  <div class="minimal-initial-investment sc-jKJlTe hZlCDP">20.000</div>
  <div class="administration-rate sc-jKJlTe hZlCDP">1,90</div>
  <div class="redemption-quotation sc-jKJlTe hZlCDP"><div>D+30<p class="sc-brqgnP fbcFSC" style="font-size: 12px; color: rgb(24, 25, 26);">(Dias Corridos)</p></div></div>
  <div class="redemption-settlement sc-jKJlTe hZlCDP"><div>D+1<p class="sc-brqgnP fbcFSC" style="font-size: 12px; color: rgb(24, 25, 26);">(Dias Úteis)</p></div></div>
  <div class="risk sc-jKJlTe hZlCDP"><span class="badge-suitability color-neutral-dark-pure sc-jWBwVP hvQuvX" style="background-color: rgb(215, 123, 10);">8<span><strong>Perfil Médio</strong><br>A nova pontuação de risco leva em consideração critérios de risco, mercado e liquidez. Para saber mais, <a href="https://conteudos.xpi.com.br/guia-de-investimentos/relatorios/pontos-de-risco/" target="_blank" title="Clique aqui para saber mais sobre a nova pontuação de risco">clique aqui</a>.</span></span></div>
  <div class="profitability sc-jKJlTe hZlCDP"><div class="sc-kEYyzF lnwNVR"></div><div class="sc-kkGfuU jBBLoV"><div class="sc-jKJlTe hZlCDP">0,92</div><div class="sc-jKJlTe hZlCDP">0,48</div><div class="sc-jKJlTe hZlCDP">5,03</div></div></div><div class="invest-button sc-jKJlTe hZlCDP"><button class="xp__button xp__button--small" data-wa="fundos-de-investimento; listagem - investir; investir Bahia AM Maraú Advisory FIC de FIM">Investir</button></div>
  <div class="arrow-details sc-jKJlTe hZlCDP"><i type="arrow" data-wa="" class="eab2eu-0 eUjuAo"></i></div></div>

HTML“箭头”是:

<div class="arrow-details sc-jKJlTe hZlCDP">
  <i type="arrow" data-wa="" class="eab2eu-0 eUjuAo">
    ::before
  </i>
</div>

【问题讨论】:

  • 这可能是几件事,但是查看加载的网页首先想到的是页面上有一个大标题。这意味着当您的刮板加载页面时,除非您滚动到该元素,否则它无法看到该元素。尝试在单击之前添加一条滚动到元素的行。这应该提供一个很好的例子:stackoverflow.com/questions/41744368/…
  • 如果这不起作用,请更新帖子,人们就会知道不要尝试该解决方案。

标签: python selenium web-scraping


【解决方案1】:

您可以检查以下代码行

option = Options()
#Disable the notification popUp
option.add_argument("--disable-notifications")
driver = webdriver.Chrome(r"ChromeDriverPath",chrome_options=option)

driver.get("https://www.xpi.com.br/investimentos/fundos-de-investimento/lista/#/")

#Clicked on the cookie, which is under the shadow-DOM So used execute_script(), Also used sleep() before clicking on cookies because at some point it throws the JS Error Shadow-DOM null.
sleep(5)
cookie_btn = driver.execute_script('return document.querySelector("#cookies-policy-container").shadowRoot.querySelector("soma-context > cookies-policy-disclaimer > div > soma-card > div > div:nth-child(2) > soma-button").shadowRoot.querySelector("button")')
cookie_btn.click()

#There can be a multiple way to scroll, Below is one of them
driver.execute_script("window.scrollBy(0,700)")

#There are multiple rows which have expand button so used the index of the XPath if you want to click on multiple try to use loop
expandir_fundo = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "((//*[@class='eab2eu-0 eUjuAo'])[1])")))
expandir_fundo.click()

导入

from time import sleep
from selenium import  webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.options import Options

【讨论】:

    【解决方案2】:

    虽然@YaDav MaNish 的回答似乎有效,但我宁愿使用自定义查询选择器点击接受 cookie 按钮,而不是由浏览器生成。

    cookie_btn = driver.execute_script('return document.querySelector("#cookies-policy-container").shadowRoot.querySelector("soma-context soma-card soma-button").shadowRoot.querySelector("button")')
    cookie_btn.click()
    

    应该可以。

    【讨论】:

      【解决方案3】:

      您的回复和 cookie 代码,为我提供了使用 execute_script 来解决我的问题的想法。

      在下面找到我的代码 这第一页我只打开 URL,删除 cookie 并展开所有信息。

      # Setting options
      options = Options()
      options.add_argument('--window-size=1900,1000')
      options.add_argument("--disable-notifications")
      
      # Opening Website
      url = 'https://www.xpi.com.br/investimentos/fundos-de-investimento/lista/#/'
      driver = webdriver.Chrome(r"chromedriver", options=options)
      driver.get(url)
      
      # Removing Cookies
      cookie_btn = driver.execute_script('return document.querySelector("#cookies-policy-container").'\
      'shadowRoot.querySelector("soma-context > cookies-policy-disclaimer > div > soma-card > div > '\
      'div:nth-child(2) > soma-button").shadowRoot.querySelector("button")')
      cookie_btn.click()
      
      # Expanding the whole list
      expandir = driver.find_elements_by_class_name("sly-row")[-4]
      expandir.click()
      sleep(4)
      
      # Creating content
      page_content = driver.page_source
      site = BeautifulSoup(page_content, 'html.parser')
      

      第二部分我使用 driver.execute_sript 在获取信息时打开箭头。

      fundos = site.find_all('div',class_='funds-table-row')
      
      for cont, fundo in enumerate(fundos):
          nome = fundo.find('div', class_='fund-name')['title']
          driver.execute_script(f"return document.getElementsByClassName('arrow-details'){[cont + 1]}.click()")
          page_detalhe = driver.page_source
          site2 = BeautifulSoup(page_detalhe, 'html.parser')
          details= site2.find('section', class_='has-documents').find_all('div')
          tax_id = details[2].get_text()
      

      感谢大家的帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-31
        相关资源
        最近更新 更多