基于問題Scraping a specific website with a search box and javascripts in Python,我試圖從網站https://www.msci.com/esg-ratings/中獲取公司評級 ,主要是在搜索框,在下拉菜單中選擇該名稱的所有選項(“RIO TINTO LIMITED”和“RIO TINTO PLC”此處為“rio tinto”),并在兩者的右上角獲得帶有評級的圖片。但是,我在處理建議公司的 ul-li 退出菜單時遇到了麻煩:from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import Byoptions = webdriver.ChromeOptions()options.add_argument('-headless')options.add_argument('-no-sandbox')options.add_argument('-disable-dev-shm-usage')options.add_argument('window-size=1920,1080')wd = webdriver.Chrome(options=options)wd.get('https://www.msci.com/esg-ratings')WebDriverWait(wd, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="_esgratingsprofile_keywords"]'))).send_keys("RIO TINTO")WebDriverWait(wd, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="ui-id-1"]/li[1]'))).click()#WebDriverWait(wd,10).until(EC.visibility_of_element_located((By.CSS_SELECTOR,"#_esgratingsprofile_esg-ratings-profile-header > div.esg-ratings-profile-header-ratingdata > div.ratingdata-container > div.ratingdata-outercircle.esgratings-profile-header-yellow > div")))print(wd.find_element_by_xpath('//*[@id="_esgratingsprofile_esg-ratings-profile-header"]/div[2]/div[1]/div[2]/div'))(代碼給出了 ElementClickInterceptedException。)如何訪問“RIO TINTO LIMITED”和“RIO TINTO PLC”所需的數據?
1 回答

慕工程0101907
TA貢獻1887條經驗 獲得超5個贊
我在處理推薦公司的 ul-li 退出菜單時遇到了麻煩
這是預期的,因為element您的目標是通過dynamic腳本呈現的。options.add_argument('-headless')為了克服這一點,你將不得不避免。
你這里也有問題
print(wd.find_element_by_xpath('//*[@id="_esgratingsprofile_esg-ratings-profile-header"]/div[2]/div[1]/div[2]/div'))
您嘗試打印元素的位置。由于目標元素是icon由呈現的CSS,因此您不能使用print()來輸出它。相反,您需要將其保存為一個.png文件
with open('filename.png', 'wb') as file:
file.write(driver.find_element_by_xpath('//*[@id="_esgratingsprofile_esg-ratings-profile-header"]/div[2]/div[1]/div[2]/div').screenshot_as_png)
然后根據您的需要使用它。
添加回答
舉報
0/150
提交
取消