I want to create a script that grabs the info from the website using selenium.
However, if it doesn't find the info and shows an error message, it skips that request and continues to the next one.
from selenium import webdriver
import pandas as pd
import undetected_chromedriver as uc
list1 = [6019306,6049500,6051161,6022230,5776662,6151430]
for x in range(0, list1.count()):
while True:
try:
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = uc.Chrome(options=options)
url = 'https://www.axie.tech/axie-pricing/' str(list1[x])
driver.get(url)
driver.implicitly_wait(10)
test = driver.find_element_by_xpath('//*[@id="root"]/div[1]/div[2]/div[2]/div/div/div[1]/div/div[1]/div[4]/div/div[3]/div/span').text
test = float(test[1:])
print(test)
driver.close()
except NoSuchElementException:
'This Value doesnt exist'
driver.close()
CodePudding user response:
A bit unclear what exactly you are trying to do through the line test = float(test[1:])
.
However to extract the desired text from the list of websites you need to induce WebDriverWait for visibility_of_element_located() and you can use the following locator strategy:
Code Block:
options = webdriver.ChromeOptions() options.add_argument("start-maximized") driver = uc.Chrome(options=options) list1 = [6019306, 6049500, 6051161, 6022230, 5776662, 6151430] for x in range(0, len(list1)): try: url = 'https://www.axie.tech/axie-pricing/' str(list1[x]) driver.get(url) print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[text()='Reasonable']//following::div[1]//span[contains(@class, 'MuiTypography-root')]"))).text) except TimeoutException: continue driver.quit()
Console Output:
Ξ0.01 Ξ0.012 Ξ0.0162 Ξ0.026
Note : You have to add the following imports :
from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException