No, it didn't help. The data is still displayed in the wrong format. The answer is below…
- User
No, it didn't help. The data is still displayed in the wrong format. The answer is below
C:\Users\AshotxXx\PycharmProjects\Parcing\pythonProject\venv\Scripts\python.exe C:\Users\AshotxXx\PycharmProjects\Parcing\pythonProject\ChromeDriver\main.py {'error': 'Failed to find the required elements or error occurred: Message: \nStacktrace:\nBacktrace:\n\tGetHandleVerifier [0x003E8893+48451]\n\t(No symbol) [0x0037B8A1]\n\t(No symbol) [0x00285058]\n\t(No symbol) [0x002B0467]\n\t(No symbol) [0x002B069B]\n\t(No symbol) [0x002DDD92]\n\t(No symbol) [0x002CA304]\ n\t(No symbol) [0x002DC482]\n\t(No symbol) [0x002CA0B6]\n\t(No symbol) [0x002A7E08]\n\t(No symbol) [0x002A8F2D]\n\tGetHandleVerifier [0x00648E3A+ 2540266]\n\tGetHandleVerifier [0x00688959+2801161]\n\tGetHandleVerifier [0x0068295C+2776588]\n\tGetHandleVerifier [0x00472280+612144]\n\t(No symbol) [0x00384F6 C]\n\t(No symbol) [0x003811D8 ]\n\t(No symbol) [0x003812BB]\n\t(No symbol) [0x00374857]\n\tBaseThreadInitThunk [0x76DF00C9+25]\n\tRtlGetAppContainerNamedObjectPath [0x77667B4E+286]\n\tRtlGetAppContainerNamedObjectPath [0x7766 7B1E+238] \n'}
Process finished with exit code 0
I repeat again, pay attention to the lines where the search id is located
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "typeahead-input-64326"))) search_box = driver.find_element_by_id("typeahead-input-64326")
The data is not registered in the search engine, because this search engine does not see the code. fix it
- Assistant
I apologize for the confusion. It seems that the issue is with finding the correct element using the id "typeahead-input-64326". In this case, please try the following steps:
- Make sure that the webpage has finished loading before executing the script. Add a
time.sleep()to add a delay in your script.
import time # ... driver.get("YOUR_URL") time.sleep(5) # Adjust the duration according to your needs- Instead of using the ID, you can try using other methods to find the element, such as by its class name, CSS selector, tag name, or XPATH. Here's an example using the class name (replace
element_class_namewith the correct class name):
search_box = driver.find_element_by_class_name("element_class_name")- If neither of the above solutions work, the element might be inside an iframe. In this case, switch to the iframe before finding the element:
# Switch to the iframe (replace `iframe_element` with the correct frame element reference) driver.switch_to.frame("iframe_element") # Find the search box and interact with it # Switch back to the main content driver.switch_to.default_content()If you still experience issues, please provide the URL of the website you're trying to scrape and any available source code, so we can take a closer look and help you more effectively.
- Make sure that the webpage has finished loading before executing the script. Add a
- User
import time from bs4 import BeautifulSoup from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys
def get_token_info(token_symbol): chrome_options = Options() chrome_options.add_argument("–headless")
# Update the path below to the actual path to your WebDriver executable service = Service( executable_path="C:\\Users\\AshotxXx\\PycharmProjects\\Parcing\\pythonProject\\ChromeDriver\\chromedriver.exe") driver = webdriver.Chrome(service=service, options=chrome_options) driver.get("https://moonarch.app/") try: # Find the search box element search = driver.find_element_by_xpath('//*[@id="typeahead-input-87261"]') # Clear the search box and type the specified token symbol search.send_keys("sfsdf") # Wait for the token info and infocard elements to appear after entering the symbol WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "token-info"))) WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "infocard"))) # Extract information using BeautifulSoup soup = BeautifulSoup(driver.page_source, "html.parser") token_info_element = soup.find("div", class_="token-info") infocard_element = soup.find("div", class_="infocard") check_alert_element = soup.find("div", class_="token-check-message check-alert") check_warning_element = soup.find("div", class_="token-check-message check-warning") check_info_element = soup.find("div", class_="token-check-message check-info") not_verified_element = soup.find("div", class_="not-verified") check_alert_status = "Yes" if check_alert_element else "None" check_warning_status = "Yes" if check_warning_element else "None" check_info_status = "Yes" if check_info_element else "None" not_verified_status = "Yes" if not_verified_element else "None" if token_info_element and infocard_element: token_name_element = token_info_element.find("span", class_="name") token_symbol_element = token_info_element.find("span", class_="symbol") info_items = infocard_element.find_all("li") if token_name_element and token_symbol_element and len(info_items) >= 7: token_name = token_name_element.text.strip() token_symbol = token_symbol_element.text.strip() price = info_items[0].find("span", class_="value").text.strip() max_supply = info_items[1].find("span", class_="value").text.strip() market_cap = info_items[2].find("span", class_="value").text.strip() liquidity = info_items[3].find("span", class_="value").text.strip() liq_mc = info_items[4].find("span", class_="value").text.strip() token_age = info_items[6].find("span", class_="value").text.strip() driver.quit() return { "name": token_name, "symbol": token_symbol, "price": price, "max_supply": max_supply, "market_cap": market_cap, "liquidity": liquidity, "liq_mc": liq_mc, "token_age": token_age, "check_alert": check_alert_status, "check_warning": check_warning_status, "check_info": check_info_status, "not_verified": not_verified_status } else: driver.quit() error_info = f"token_name_element: {token_name_element}, token_symbol_element: {token_symbol_element}, info_items count: {len(info_items)}" return {"error": f"Failed to find the required info items or name and.symbol. Error_info: {error_info}"} else: driver.quit() return {"error": f"Failed to find the required elements for token_info_element ({token_info_element}) and infocard_element ({infocard_element})."} except Exception as e: driver.quit() return {"error": f"Failed to find the required elements or error occurred: {e}"}token_symbol = "0x0E481Fa712201f61dAf97017138Efaa69e2A3df3" print(get_token_info(token_symbol))
Fix the error in the code above. The error is listed below.
C:\Users\AshotxXx\PycharmProjects\Parcing\pythonProject\venv\Scripts\python.exe C:\Users\AshotxXx\PycharmProjects\Parcing\pythonProject\ChromeDriver\main.py {'error': "Failed to find the required elements or error occurred: 'WebDriver' object has no attribute 'find_element_by_xpath'"}
Process finished with exit code 0
Then write the complete code.
- Assistant
The error you're seeing
'WebDriver' object has no attribute 'find_element_by_xpath'is becausefind_element_by_xpath()function does not exist. The correct function name isfind_element(By.XPATH, value). Here's the updated code:import time from bs4 import BeautifulSoup from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys def get_token_info(token_symbol): chrome_options = Options() chrome_options.add_argument("–headless") service = Service( executable_path="C:\\Users\\AshotxXx\\PycharmProjects\\Parcing\\pythonProject\\ChromeDriver\\chromedriver.exe") driver = webdriver.Chrome(service=service, options=chrome_options) driver.get("https://moonarch.app/") try: # Find the search box element using xpath search = driver.find_element(By.XPATH, '//*[@id="typeahead-input-87261"]') # Clear the search box and type the specified token symbol search.send_keys("sfsdf") # Wait for the token info and infocard elements to appear after entering the symbol WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "token-info"))) WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "infocard"))) # Extract information using BeautifulSoup soup = BeautifulSoup(driver.page_source, "html.parser") token_info_element = soup.find("div", class_="token-info") infocard_element = soup.find("div", class_="infocard") check_alert_element = soup.find("div", class_="token-check-message check-alert") check_warning_element = soup.find("div", class_="token-check-message check-warning") check_info_element = soup.find("div", class_="token-check-message check-info") not_verified_element = soup.find("div", class_="not-verified") check_alert_status = "Yes" if check_alert_element else "None" check_warning_status = "Yes" if check_warning_element else "None" check_info_status = "Yes" if check_info_element else "None" not_verified_status = "Yes" if not_verified_element else "None" if token_info_element and infocard_element: token_name_element = token_info_element.find("span", class_="name") token_symbol_element = token_info_element.find("span", class_="symbol") info_items = infocard_element.find_all("li") if token_name_element and token_symbol_element and len(info_items) >= 7: token_name = token_name_element.text.strip() token_symbol = token_symbol_element.text.strip() price = info_items[0].find("span", class_="value").text.strip() max_supply = info_items[1].find("span", class_="value").text.strip() market_cap = info_items[2].find("span", class_="value").text.strip() liquidity = info_items[3].find("span", class_="value").text.strip() liq_mc = info_items[4].find("span", class_="value").text.strip() token_age = info_items[6].find("span", class_="value").text.strip() driver.quit() return { "name": token_name, "symbol": token_symbol, "price": price, "max_supply": max_supply, "market_cap": market_cap, "liquidity": liquidity, "liq_mc": liq_mc, "token_age": token_age, "check_alert": check_alert_status, "check_warning": check_warning_status, "check_info": check_info_status, "not_verified": not_verified_status } else: driver.quit() error_info = f"token_name_element: {token_name_element}, token_symbol_element: {token_symbol_element}, info_items count: {len(info_items)}" return {"error": f"Failed to find the required info items or name and.symbol. Error_info: {error_info}"} else: driver.quit() return {"error": f"Failed to find the required elements for token_info_element ({token_info_element}) and infocard_element ({infocard_element})."} except Exception as e: driver.quit() return {"error": f"Failed to find the required elements or error occurred: {e}"} token_symbol = "0x0E481Fa712201f61dAf97017138Efaa69e2A3df3" print(get_token_info(token_symbol))With this change, the code should work correctly. Please make sure to replace the sample
token_symbolwith the one you want to use. - User
Switch to the iframe (replace iframe_element with the correct frame element reference)
driver.switch_to.frame(“iframe_element”)
Find the search box and interact with it
Switch back to the main content
driver.switch_to.default_content()
Explain step by step how to switch to iframe to enter a specific parameter in the search box
- Assistant
Switching to an iframe and interacting with elements inside can be done in the following steps:
- Locate the iframe element: Use one of the locator strategies (like ID, class name, or xpath) to find the iframe element on the main page. For example, if the iframe has an ID
my_iframe, use:
iframe_element = driver.find_element(By.ID, 'my_iframe')- Switch to the iframe: Use the
switch_to.frame()method to switch the WebDriver's context to the iframe:
driver.switch_to.frame(iframe_element)- Interact with elements inside the iframe: Once you have switched to the iframe, you can locate the search box (or any other element) using similar strategies as used earlier. For example, if the search box has an ID
search_box, use:
search_box = driver.find_element(By.ID, 'search_box') search_box.send_keys('your_search_query')- Switch back to the main content: After interacting with the iframe, you may want to switch back to the main content to continue interacting with elements on the main page. Use the
switch_to.default_content()method:
driver.switch_to.default_content()Here's a full example:
# Locate the iframe element iframe_element = driver.find_element(By.ID, 'my_iframe') # Switch to the iframe driver.switch_to.frame(iframe_element) # Find the search box and interact with it (replace 'your_search_query' with the query you want to use) search_box = driver.find_element(By.ID, 'search_box') search_box.send_keys('your_search_query') # Switch back to the main content driver.switch_to.default_content()Replace
my_iframe,search_box, andyour_search_querywith the actual values you want to use. - Locate the iframe element: Use one of the locator strategies (like ID, class name, or xpath) to find the iframe element on the main page. For example, if the iframe has an ID