随机硒错误(元素点击被拦截:其他元素会收到点击)



我正在尝试从以下页面获取数据:https://www.robeco.com/ch/en/funds/prof-ch-en-11/robeco-qi-emerging-conservative-equities-f-eur-lu0940007007.html

我使用以下脚本:

url="https://www.robeco.com/ch/en/funds/prof-ch-en-11/robeco-qi-emerging-conservative-equities-f-eur-lu0940007007.html"
driver.get(url)
time.sleep(5)
xpath1='/html/body/div[3]/div/div/div[2]/button'
driver.find_element(By.XPATH, xpath1).click()
time.sleep(2)
xpath2='/html/body/div[4]/div/div/div/div/ul/li'
driver.find_element(By.XPATH, xpath2).click()

第一次单击接受一般条件免责声明。第二次点击接受cookie免责声明。我在第二次点击时遇到了问题,因为有时会出现以下错误。这种行为看起来是随机的:有时脚本有效,有时会出现错误。

ElementClickInterceptedException: Message: element click intercepted: Element <li class="btn" id="sdgdpr_modal_buttons-agree">...</li> is not clickable at point (790, 521). Other element would receive the click: <p>...</p>
(Session info: chrome)
Stacktrace:
Backtrace:
Ordinal0 [0x00F1DF13+2219795]
Ordinal0 [0x00EB2841+1779777]
Ordinal0 [0x00DC423D+803389]
Ordinal0 [0x00DF99D4+1022420]
Ordinal0 [0x00DF78C4+1013956]
Ordinal0 [0x00DF54AB+1004715]
Ordinal0 [0x00DF4117+999703]
Ordinal0 [0x00DE9B76+957302]
Ordinal0 [0x00E0E7FC+1107964]
Ordinal0 [0x00DE94B4+955572]
Ordinal0 [0x00E0EA14+1108500]
Ordinal0 [0x00E1F192+1175954]
Ordinal0 [0x00E0E616+1107478]
Ordinal0 [0x00DE7F89+950153]
Ordinal0 [0x00DE8F56+954198]
GetHandleVerifier [0x01212CB2+3040210]
GetHandleVerifier [0x01202BB4+2974420]
GetHandleVerifier [0x00FB6A0A+565546]
GetHandleVerifier [0x00FB5680+560544]
Ordinal0 [0x00EB9A5C+1808988]
Ordinal0 [0x00EBE3A8+1827752]
Ordinal0 [0x00EBE495+1827989]
Ordinal0 [0x00EC80A4+1867940]
BaseThreadInitThunk [0x75EFFA29+25]
RtlGetAppContainerNamedObjectPath [0x77C17B5E+286]
RtlGetAppContainerNamedObjectPath [0x77C17B2E+238]

我能做什么?感谢

您的代码有几个问题:

  1. 您需要学习如何创建正确的定位器。绝对XPath极其脆弱且不可靠
  2. 您应该使用WebDriverWaitexpected_conditions,而不是硬编码的暂停
  3. 在第二个cookie横幅上,您应该单击父ul元素,而不是子li元素

以下代码有效:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:webdriverschromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)
url = "https://www.robeco.com/ch/en/funds/prof-ch-en-11/robeco-qi-emerging-conservative-equities-f-eur-lu0940007007.html"
driver.get(url)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "btn-turquoise"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, 'sdgdpr_modal_buttons'))).click()

相关内容

  • 没有找到相关文章

最新更新