[英]Unknown Python Selenium error:Traceback (most recent call last): File "D:\pythonProject\webscraping.py", line 17, in <module>
I am making this little program for Minecraft as a little program for fun and practicing python.我正在为 Minecraft 制作这个小程序,作为一个有趣和练习 python 的小程序。 I am using selenium and when I was trying to get the name of something in the HTML webpage with find_element it gave me this massive error code:我正在使用 selenium,当我尝试使用 find_element 在 HTML 网页中获取某些内容的名称时,它给了我这个巨大的错误代码:
Traceback (most recent call last):
File "D:\pythonProject\webscraping.py", line 17, in <module>
username = driver.find_element(By.NAME, 'text-shadow: 1px 1px #eee; color:#3CE6E6')
File "C:\Users\Han\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 1253, in find_element
'value': value})['value']
File "C:\Users\Han\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 430, in execute
self.error_handler.check_response(response)
File "C:\Users\Han\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, 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":"[name="text-shadow: 1px 1px #eee; color:#3CE6E6"]"}
(Session info: chrome=103.0.5060.53)
Stacktrace:
Backtrace:
Ordinal0 [0x00DB56B3+2184883]
Ordinal0 [0x00D4E5F1+1762801]
Ordinal0 [0x00C63DA8+802216]
Ordinal0 [0x00C91B40+990016]
Ordinal0 [0x00C91DDB+990683]
Ordinal0 [0x00CBEC32+1174578]
Ordinal0 [0x00CACB54+1100628]
Ordinal0 [0x00CBCF52+1167186]
Ordinal0 [0x00CAC926+1100070]
Ordinal0 [0x00C86EA0+945824]
Ordinal0 [0x00C87D96+949654]
GetHandleVerifier [0x01054192+2704034]
GetHandleVerifier [0x0104687D+2648461]
GetHandleVerifier [0x00E4119A+529066]
GetHandleVerifier [0x00E40006+524566]
Ordinal0 [0x00D54F2B+1789739]
Ordinal0 [0x00D59978+1808760]
Ordinal0 [0x00D59A65+1808997]
Ordinal0 [0x00D62A01+1845761]
BaseThreadInitThunk [0x74F1FA29+25]
RtlGetAppContainerNamedObjectPath [0x77097A9E+286]
RtlGetAppContainerNamedObjectPath [0x77097A6E+238]
Here is my code for the program:这是我的程序代码:
from tqdm import tqdm
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
x=Service('C:\Program Files (x86)\chromedriver.exe')
driver = webdriver.Chrome(service=x)
pbar = tqdm(range(10))
for i in pbar:
time.sleep(0.3)
pbar.set_description("loading program")
PlankeName = input("Whos Planke Page would you like to view? ")
UserPlankeLink = "https://plancke.io/hypixel/player/stats/" + PlankeName
driver.get(UserPlankeLink)
username = driver.find_element(By.NAME, 'text-shadow: 1px 1px #eee; color:#3CE6E6')
print(username)
print("You are currently Viewing " + driver.title)
Please help!请帮忙!
Your error is NoSuchElementException
because your element is not found.您的错误是NoSuchElementException
因为找不到您的元素。 This exists because your identifier is wrong, or the page has not fully loaded yet.这是因为您的标识符错误,或者页面尚未完全加载。
I've had a look at your page - and your identifier is wrong.我看过你的页面 - 你的标识符是错误的。 But - It's also a poorly described website so it's a tricky one for beginners.但是 - 这也是一个描述不佳的网站,因此对于初学者来说是一个棘手的网站。
I updated it to use this xpath:我将其更新为使用此 xpath:
//div[h3[text()='Player Information']]//span[text()]
That is: From the div
, which has the h3
inside it and where that h3
has text=player information - Get the span which has any text.即:从div
中,其中包含h3
并且h3
具有 text=player 信息 - 获取包含任何文本的 span。
Using this this code:使用此代码:
driver = webdriver.Chrome() # note i modified this to my driver - reset to yours
driver.implicitly_wait(10)
PlankeName = input("Whos Planke Page would you like to view? ")
UserPlankeLink = "https://plancke.io/hypixel/player/stats/" + PlankeName
driver.get(UserPlankeLink)
username = driver.find_element(By.XPATH, "//div[h3[text()='Player Information']]//span[text()]")
print(username.text)
print("You are currently Viewing " + driver.title)
When entering the input test
as the players name, I get output:当输入输入test
作为玩家名称时,我得到输出:
You are currently Viewing Test's Stats | Plancke
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.