English 中文(简体)
我如何利用 se提取文章的数量?
原标题:How do I extract number of articles using selenium?
from selenium import webdriver
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
driver.get("https://en.wikipedia.org/wiki/Main_Page")
driver.implicitly_wait(5)
statistic=driver.find_element(By.CSS_SELECTOR,value=".articlecount a")
print(statistic.text)

我试图从Wikipedia拆解一些文章,但Im却发现这一错误。 我不敢肯定会做些什么,因为选择名称正确,我增加了等候时间。

Traceback (most recent call last):
  File "C:Users38161PycharmProjectspythonProject5inspection.py", line 7, in <module>
    statistic=driver.find_element(By.CSS_SELECTOR,value=".articlecount a")
  File "C:Users38161PycharmProjectspythonProject5venvlibsite-packagesseleniumwebdriver
emotewebdriver.py", line 742, in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
  File "C:Users38161PycharmProjectspythonProject5venvlibsite-packagesseleniumwebdriver
emotewebdriver.py", line 348, in execute
    self.error_handler.check_response(response)
  File "C:Users38161PycharmProjectspythonProject5venvlibsite-packagesseleniumwebdriver
emoteerrorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".articlecount a"}
  (Session info: chrome=120.0.6099.109); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Stacktrace:
    GetHandleVerifier [0x00007FF723BC4D02+56194]
    (No symbol) [0x00007FF723B304B2]
    (No symbol) [0x00007FF7239D76AA]
    (No symbol) [0x00007FF723A216D0]
    (No symbol) [0x00007FF723A217EC]
    (No symbol) [0x00007FF723A64D77]
    (No symbol) [0x00007FF723A45EBF]
    (No symbol) [0x00007FF723A62786]
    (No symbol) [0x00007FF723A45C23]
    (No symbol) [0x00007FF723A14A45]
    (No symbol) [0x00007FF723A15AD4]
    GetHandleVerifier [0x00007FF723F3D5BB+3695675]
    GetHandleVerifier [0x00007FF723F96197+4059159]
    GetHandleVerifier [0x00007FF723F8DF63+4025827]
    GetHandleVerifier [0x00007FF723C5F029+687785]
    (No symbol) [0x00007FF723B3B508]
    (No symbol) [0x00007FF723B37564]
    (No symbol) [0x00007FF723B376E9]
    (No symbol) [0x00007FF723B28094]
    BaseThreadInitThunk [0x00007FFC06607344+20]
    RtlUserThreadStart [0x00007FFC067426B1+33]
问题回答

2. 改变这一方向

statistic=driver.find_element(By.CSS_SELECTOR,value=".articlecount a")

:

statistic=driver.find_element(By.CSS_SELECTOR,value="#articlecount a")

您正在使用<条码>,,但您正在使用一些物色器,而不必使用<条码>。

from selenium import webdriver
from selenium.webdriver.common.by import By
driver=webdriver.Firefox()
driver.get("https://en.wikipedia.org/wiki/Main_Page")
driver.implicitly_wait(5)
statistic=driver.find_element(By.CSS_SELECTOR,value="#articlecount a")
print(statistic.text)

你们可以使用ID。

import time
from selenium import webdriver
from selenium.webdriver.common.by import By

driver= webdriver.Chrome()
driver.get("https://en.wikipedia.org/wiki/Main_Page")
time.sleep(10)
count = driver.find_element(By.ID, "articlecount").text
print(count.split(" ")[0])




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签