为什么我在写了这行下面的东西后会出错?

2024-10-02 08:24:23 发布

您现在位置:Python中文网/ 问答频道 /正文

import pandas as pd
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from pandas.io.html import read_html
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys 
import time
from bs4 import BeautifulSoup

url = 'https://www.globenewswire.com/Search/NewsSearch?keyword=complete%20response%20letter&icb=4570&subjectCode=Company%20Announcement'

def CRL_Updater():
    options = Options()
    options.headless = False
    driver = webdriver.Chrome('/Users/sajjad/Downloads/chromedriver', options=options)
    driver.get(url)
    driver.find_element_by_xpath('/html/body/div[2]/div/a[1]/i[1]').click()
    time.sleep(0.5)
    search_bar = driver.find_element_by_xpath('//*[@id="quicksearch-textbox"]')
    search_bar.send_keys('complete response letter')
    search_bar.send_keys(Keys.ENTER)
    time.sleep(1)
    industry_box = driver.find_element_by_id('facetfield_Icb_4570').click()
    time.sleep(1)
    subject_box =  driver.find_element_by_id('facetfield_SubjectCode_Company_Announcement').click()

    


CRL_Updater()

这是我的密码。基本上每次我在subject_box = driver.find_element_by_id('facetfield_SubjectCode_Company_Announcement').click()行之后写任何东西时,都会出现以下错误

File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <input type="checkbox" id="facetfield_Icb_4570" name="facetfield_Icb" value="4570"> is not clickable at point (27, 588). Other element would receive the click: <div class="action-container">...</div>
  (Session info: chrome=87.0.4280.88)

我以前在其他网站上使用过selenium,也做过类似的事情,但我从来没有遇到过这样的问题。似乎每次我在那句话之后加上任何东西,我都会犯这个错误,我不完全确定为什么。当我添加下一步要做的事情时,我的代码会一直占用我直到复选框行,并在那里停止,声称它不会单击。为什么它可以在我添加更多代码之前单击它,而不能在之后单击它


Tags: fromimportdividbytimedriverselenium
2条回答

这取决于页面上发生了什么,因为该元素可能被另一个元素截获或未正确加载;在这种情况下,元素更有可能被截获,但为了避免这两种情况,您可以结合使用WebDriverWait和Javascript,以等待元素可单击,并且即使被截获也可以单击元素:

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

subject_box =  WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.ID, 'facetfield_SubjectCode_Company_Announcement')))
driver.execute_script("arguments[0].click();", subject_box)

单击该元素时,该元素可能尚未就绪。 试试这个

import pandas as pd
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from pandas.io.html import read_html
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys 
import time
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

url = 'https://www.globenewswire.com/Search/NewsSearch?keyword=complete%20response%20letter&icb=4570&subjectCode=Company%20Announcement'

def CRL_Updater():
    options = Options()
    options.headless = False
    driver = webdriver.Chrome('/Users/sajjad/Downloads/chromedriver', options=options)
    driver.get(url)
    driver.find_element_by_xpath('/html/body/div[2]/div/a[1]/i[1]').click()
    time.sleep(0.5)
    search_bar = driver.find_element_by_xpath('//*[@id="quicksearch-textbox"]')
    search_bar.send_keys('complete response letter')
    search_bar.send_keys(Keys.ENTER)
    time.sleep(1)
    industry_box = driver.find_element_by_id('facetfield_Icb_4570').click()
    time.sleep(1)
    subject_box = WebDriverWait(driver, 20).until(
    EC.element_to_be_clickable((By.ID, "facetfield_SubjectCode_Company_Announcement")))
    subject_box.click()


CRL_Updater()

相关问题 更多 >

    热门问题