I'm trying to check if a webpage has a certain element with a try/catch function, and then, depending on the result go thru a loop. Not quite working for me. I get a time out exception on the imgsrc3 line. Probably something obvious but I'm just not getting it!
import requests
from bs4 import BeautifulSoup
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import time
from splinter import Browser
from selenium.common.exceptions import NoSuchElementException
# driver_path = '/usr/local/bin/chromedriver'
# driver = webdriver.Chrome(executable_path=driver_path)
browser = Browser('firefox')
options = Options()
driver = webdriver.Firefox(options=options)
driver.get("https://superrare.com/clairesalvo")
time.sleep(5)
def check_exists_by_xpath(xpath):
try:
WebDriverWait(driver,35).until(EC.presence_of_all_elements_located((By.XPATH, "//div[contains(@class,'profile__bio']")))
except NoSuchElementException:
return False
return True
while(True):
imgsrc3 = WebDriverWait(driver,50).until(EC.presence_of_all_elements_located((By.XPATH, "//div[contains(@class,'profile__bio']")))
for i in imgsrc3:
bio = i.text
else:
bio = "none"
CodePudding user response:
I believe your xpath filter should be:
//div[@class='profile__bio']
Also, you don't seem to use the function check_exists_by_xpath that you decalared.
And your while(True) loop will never exit.
CodePudding user response:
You've created a function but not invoked it.
def check_exists_by_xpath(xpath):
try:
WebDriverWait(driver,35).until(EC.presence_of_all_elements_located((By.XPATH, xpath)))
except NoSuchElementException:
return False
return True
if check_exists_by_xpath("//div[contains(@class,'profile__bio')]"):
imgsrc3 = WebDriverWait(driver,50).until(EC.presence_of_all_elements_located((By.XPATH, "//div[contains(@class,'profile__bio')]")))
for i in imgsrc3:
bio = i.text
else:
bio = "none"
CodePudding user response:
There are a few things in your code that I would like to point out:
Not invoking your function
You are creating your function but you are never invoking it
Identation problem
there is an identation problem, this line
while(True):
and all the code after it, is out of the check_exists_by_xpath function.
Unused parameter
you are passing xpath parameter that you are not using anywhere in the function:
def check_exists_by_xpath(xpath):
Infinite loop
If you use while(True): you are creating an infinite loop that is never gonna stop unless you use a break inside it
while(True):
imgsrc3 = WebDriverWait(driver,50).until(EC.presence_of_all_elements_located((By.XPATH, "//div[contains(@class,'profile__bio']")))
for i in imgsrc3:
bio = i.text
