英文:
NoSuchElementException Error in Selenium Python
问题
I can help you with the translation of the code portion you provided. Here is the translated code:
from selenium import webdriver
from selenium.webdriver.common.by import By
import pandas as pd
Name = "/html/body/div[2]/div/div[3]/div/div/div[2]/div[2]/div/div[4]/div/div/div/div[1]/div/div/div/div[2]/div[1]/table/tbody/tr[3]/td[2]/span"
Address = "/html/body/div[2]/div/div[3]/div/div/div[2]/div[2]/div/div[4]/div/div/div/div[1]/div/div/div/div[2]/div[2]/table/tbody/tr[4]/td[2]"
StationModel = "/html/body/div[2]/div/div[3]/div/div/div[2]/div[2]/div/div[4]/div/div/div/div[1]/div/div/div/div[2]/div[4]/table/tbody/tr[1]/td[2]"
SocketType = ""
path_chrome = r'C:\Users\efede\OneDrive\Masaüstü\F\exeler\chromedriver.exe'
path_firefox = r'C:\Users\efede\OneDrive\Masaüstü\F\exeler\geckodriver.exe'
driver = webdriver.Chrome(path_chrome)
NameList = list()
AddressList = list()
StationModelList = list()
driver.get('https://esarj.com/harita?s=TR-BOL-001')
names = driver.find_elements(By.XPATH, Name)
adresses = driver.find_element(By.XPATH, Address)
models = driver.find_element(By.XPATH, StationModel)
driver.close()
print(names)
zip_df = pd.DataFrame([NameList, AddressList, StationModelList], columns=["İstasyonAdı", "Adres", "İstasyonModeli"])
zip_df.to_excel("ESARJ.xlsx", sheet_name="ESARJ(12.6.23)")
Please note that I have translated the code, but it's important to ensure that the website structure and XPaths are correct for your specific use case, as they can change over time. Also, make sure you have the necessary WebDriver executable files (chromedriver.exe or geckodriver.exe) in the specified paths.
英文:
My main goal is extract information from a website that contains name,address,socket-type,station-model parameters from map of website.I decided to extract it with Selenium with python. At first i extracted xpaths of all html elements that i intended to extract information from and i used driver.find_elements
method but after i run code, i get NoSuchElementException error.
My Code:
from selenium import webdriver
from selenium.webdriver.common.by import By
Name="/html/body/div[2]/div/div[3]/div/div/div[2]/div[2]/div/div[4]/div/div/div/div[1]/div/div/div/div[2]/div[1]/table/tbody/tr[3]/td[2]/span"
Address="/html/body/div[2]/div/div[3]/div/div/div[2]/div[2]/div/div[4]/div/div/div/div[1]/div/div/div/div[2]/div[2]/table/tbody/tr[4]/td[2]"
StationModel="/html/body/div[2]/div/div[3]/div/div/div[2]/div[2]/div/div[4]/div/div/div/div[1]/div/d iv/div/div[2]/div[4]/table/tbody/tr[1]/td[2]"
SocketType=""
path_chrome = r'C:\Users\efede\OneDrive\Masaüstü\F\exeler\chromedriver.exe'
path_firefox=r'C:\Users\efede\OneDrive\Masaüstü\F\exeler\geckodriver.exe'
driver = webdriver.Chrome(path_chrome)
NameList=list()
AddressList=list()
StationModelList=list()
driver.get('https://esarj.com/harita?s=TR-BOL-001')
names=driver.find_elements(By.XPATH,Name)
adresses=driver.find_element(By.XPATH,Address)
models=driver.find_element(By.XPATH,StationModel)
driver.close()
print(names)
zip_df=pd.DataFrame([NameList,AddressList,StationModelList],columns["İstasyonAdı","Adres","İstasyonModeli"])
zip_df.to_excel("ESARJ.xlsx",sheet_name="ESARJ(12.6.23)")
and i got this error:
DevTools listening on ws://127.0.0.1:49202/devtools/browser/5844b146-cafa-45a9-bdfb-bc07e721d63d
Traceback (most recent call last):
File "c:\Users\efede\OneDrive\Masaüstü\F\Scrapping\esarj.py", line 27, in <module>
adresses=driver.find_element(By.XPATH,Address)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\efede\OneDrive\Masaüstü\F\Scrapping\scrappingenv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 831, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\efede\OneDrive\Masaüstü\F\Scrapping\scrappingenv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "C:\Users\efede\OneDrive\Masaüstü\F\Scrapping\scrappingenv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[2]/div/div[3]/div/div/div[2]/div[2]/div/div[4]/div/div/div/div[1]/div/div/div/div[2]/div[2]/table/tbody/tr[4]/td[2]"}
(Session info: chrome=114.0.5735.110)
答案1
得分: 0
Here's the translated code section:
从您提出问题的有限理解中,我已经编写了下面的可运行代码。请参考以下内容。
**注意**,我已经通过删除“绝对XPath”并替换为“相对XPath”,以及应用“等待”来重构了您的代码。
```python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Name = "//td[text()='Şarj Noktası:']//following::span[1]"
Address = "//td[text()='Adres:']//following::td[1]"
StationModel = "//td[text()='İstasyon Modeli:']//following::td[1]"
SocketType = ""
path_chrome = r'C:\Users\efede\OneDrive\Masaüstü\F\exeler\chromedriver.exe'
path_firefox = r'C:\Users\efede\OneDrive\Masaüstü\F\exeler\geckodriver.exe'
driver = webdriver.Chrome(path_chrome)
driver.maximize_window()
wait = WebDriverWait(driver, 30)
driver.get('https://esarj.com/harita?s=TR-BOL-001')
# 接受Cookie
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'Tamam')]"))).click()
names = wait.until(EC.visibility_of_element_located((By.XPATH, Name))).text
# 点击Lokasyon选项卡
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Lokasyon']"))).click()
adresses = wait.until(EC.visibility_of_element_located((By.XPATH, Address))).text
# 点击Teknik选项卡
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Teknik']"))).click()
models = wait.until(EC.visibility_of_element_located((By.XPATH, StationModel))).text
print(names)
print(adresses)
print(models)
driver.close()
控制台结果:
Highway Outlet Alışveriş ve Yaşam Merkezi
Ankara-İstanbul Otoyolu, 227. Km, Elmalık Mevkii, 14030 Bolu / Türkiye
PM 22kVA
进程以退出代码0完成
英文:
From the limited understanding of your question, I have written down a working code below. Please refer below.
Note that I have refactored your code by removing absolute XPaths
and replacing them with relative XPaths
and also by applying waits
.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Name = "//td[text()='Şarj Noktası:']//following::span[1]"
Address = "//td[text()='Adres:']//following::td[1]"
StationModel = "//td[text()='İstasyon Modeli:']//following::td[1]"
SocketType = ""
path_chrome = r'C:\Users\efede\OneDrive\Masaüstü\F\exeler\chromedriver.exe'
path_firefox=r'C:\Users\efede\OneDrive\Masaüstü\F\exeler\geckodriver.exe'
driver = webdriver.Chrome(path_chrome)
driver.maximize_window()
wait = WebDriverWait(driver, 30)
driver.get('https://esarj.com/harita?s=TR-BOL-001')
# Accept cookie
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'Tamam')]"))).click()
names = wait.until(EC.visibility_of_element_located((By.XPATH, Name))).text
# click on Lokasyon tab
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Lokasyon']"))).click()
adresses = wait.until(EC.visibility_of_element_located((By.XPATH, Address))).text
# click on Teknik tab
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Teknik']"))).click()
models = wait.until(EC.visibility_of_element_located((By.XPATH, StationModel))).text
print(names)
print(adresses)
print(models)
driver.close()
Console Result:
Highway Outlet Alışveriş ve Yaşam Merkezi
Ankara-İstanbul Otoyolu, 227. Km, Elmalık Mevkii, 14030 Bolu / Türkiye
PM 22kVA
Process finished with exit code 0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论