首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复硒中的NoSuchElementException?

如何修复硒中的NoSuchElementException?
EN

Stack Overflow用户
提问于 2021-07-20 23:50:25
回答 1查看 108关注 0票数 0

我正在寻找尽可能多的数据,关于电动车公司在压缩基地,从名称,工业,总部位置。除了每一次,我尝试遇到一个问题:首先我遇到了NoSuchElementException,所以我添加了一些WebDriverWait代码,然后我遇到了一个"NameError: name 'By‘into“,修复了这个问题;由于某种原因,我现在又得到了"NoSuchElementException”。我该怎么解决这个问题?如有任何帮助,将不胜感激!)

代码语言:javascript
复制
###Import Selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

#set driver path
PATH = "C:/Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://www.crunchbase.com/search/organizations/field/organization.companies/categories/electric-vehicle")

print(driver.title)
#wait to find element
WebDriverWait(driver, 20).until(
        EC.visibility_of_element_located(
          (By.XPATH, ('/html/body/chrome/div/mat-sidenav-container/mat-sidenav-content/div/search/page-layout/div/div/form/div[2]/results/div/div/div[3]/sheet-grid/div/div/grid-body/div/grid-row[1]/grid-cell[2]/div/field-formatter/identifier-formatter/a/div/div')
        )))


companies = driver.find_elements_by_class_name('ng-star-inserted')
#iterate through companies for data
for company in companies:
    name = company.find_element_by_xpath('./html/body/chrome/div/mat-sidenav-container/mat-sidenav-content/div/search/page-layout/div/div/form/div[2]/results/div/div/div[3]/sheet-grid/div/div/grid-body/div/grid-row[1]/grid-cell[2]/div/field-formatter/identifier-formatter/a/div/div').text
    industry = company.find_element_by_xpath('./html/body/chrome/div/mat-sidenav-container/mat-sidenav-content/div/search/page-layout/div/div/form/div[2]/results/div/div/div[3]/sheet-grid/div/div/grid-body/div/grid-row[1]/grid-cell[3]/div/field-formatter/identifier-multi-formatter').text
    hq = company.find_element_by_xpath('./html/body/chrome/div/mat-sidenav-container/mat-sidenav-content/div/search/page-layout/div/div/form/div[2]/results/div/div/div[3]/sheet-grid/div/div/grid-body/div/grid-row[1]/grid-cell[4]/div/field-formatter/identifier-multi-formatter/span').text
    print(name,industry,hq)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-21 08:19:01

您可以尝试使用以下代码:

代码语言:javascript
复制
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://www.crunchbase.com/search/organizations/field/organization.companies/categories/electric-vehicle")
wait = WebDriverWait(driver, 10)
print(driver.title)

name = []
industry = []
hq = []
industry_counter = 1
hq_counter = 2
companies = driver.find_elements_by_css_selector("div.identifier-label")
for company in companies:
    name.append(company.text)
    industry.append(wait.until(EC.presence_of_element_located((By.XPATH, f"(//div[contains(text(), 'Industries')]/ancestor::grid-header/following-sibling::grid-body/descendant::grid-row/grid-cell/descendant::identifier-multi-formatter)[{industry_counter}]"))).text)
    hq.append(wait.until(EC.presence_of_element_located((By.XPATH, f"(//div[contains(text(), 'Industries')]/ancestor::grid-header/following-sibling::grid-body/descendant::grid-row/grid-cell/descendant::identifier-multi-formatter)[{hq_counter}]"))).text)
    industry_counter = industry_counter + 2
    hq_counter = hq_counter  + 2

print(name)
print(industry)
print(hq)

导入:

代码语言:javascript
复制
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

输出:

代码语言:javascript
复制
Query Builder | Organizations | Crunchbase
['Lucid Motors', 'ChargePoint', 'NIO', 'Lordstown Motors', 'Tesla', 'Bird', 'Helbiz', 'Joby Aviation', 'TIER Mobility', 'Solid Power', 'VOI Technology', 'Volta Trucks', 'Xpeng Motors', 'Ideanomics', 'Lightyear']
['Automotive, Autonomous Vehicles, Electric Vehicle,', 'Automotive, Electric Vehicle, Manufacturing,', 'Automotive, Autonomous Vehicles, Electric Vehicle,,,', 'Automotive, Electric Vehicle, Industrial,', 'Automotive, Autonomous Vehicles, Electric Vehicle,', 'Electric Vehicle, Mobile, Mobile Apps,,', 'Electric Vehicle, Last Mile Transportation,', 'Aerospace, Air Transportation, Electric Vehicle,', 'Electric Vehicle, GreenTech, Last Mile Transportation,', 'Battery, Electric Vehicle, Energy, Energy Storage,', 'Electric Vehicle, Public Transportation,,', 'Automotive, Electric Vehicle, Last Mile Transportation,', 'Automotive, Autonomous Vehicles, Electric Vehicle,', 'Digital Marketing, Electric Vehicle, Financial Services,,', 'Automotive, Electric Vehicle, Energy, Manufacturing,']
['Newark, California, United States', 'Campbell, California, United States', 'Shanghai, Shanghai, China', 'Warren, Ohio, United States', 'Palo Alto, California, United States', 'Santa Monica, California, United States', 'New York, New York, United States', 'Santa Cruz, California, United States', 'Berlin, Berlin, Germany', 'Louisville, Colorado, United States', 'Stockholm, Stockholms Lan, Sweden', 'Sigtuna, Stockholms Lan, Sweden', 'Guangzhou, Guangdong, China', 'New York, New York, United States', 'Helmond, Noord-Brabant, The Netherlands']
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68462520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档