I am scraping a website for data from a table, which is loaded via AJAX. The website is slow, and inconsistent, so sometimes I have to wait <5 sec for the table to load, while other times I have to wait 25 - 30. I am iterating through hundreds of items that filter the table, and once loaded, I go to the next item.
The functionality of the Explicit Wait / Expected Conditions does not seem to be behaving as I expect and wondered if anyone might have some insight.
I have tried numerous approaches to the problem, which I seem to have a different exception each time I run it.
This first snippet is to keep trying until it finds the element. I want to continue running until the page is fully loaded and the element is found. The problem is, the page is still loading and the element hasn't been found yet, but it still throws an exception.
for s in range(0,1000):
try:
#Other Month Value Clicked
wait.until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[9]/div/div[2]/div[" + str(mths[x]) + "]")))
except NoSuchElementException:
print(".", end=".", flush=True)
time.sleep(1)
timePeriodVal.click()
time.sleep(1)
timePeriodVal.click()
continue
finally:
timePeriod = (driver.find_element_by_xpath("/html/body/div[9]/div/div[2]/div[" + str(mths[x]) + "]"))
timePeriod.click()
#print('\nTime Period clicked')
time.sleep(1.5)
break
Related
for i in range(len(tlds)):
#print("TLD at index " + str(i) + " is " + tlds[i])
try:
print("Cerca sito...")
sito = WebDriverWait(driver, 0.2).until(EC.presence_of_element_located((By.XPATH, "//div[contains(text(),'" + str(tlds[i]) +"') and #class ='Io6YTe fontBodyMedium']"))).text
except:
#print("T1")
sito = None
if sito is not None:
print("T2")
break
Here is the code: the part that is taking me so long is sito=... it cycles correctly through the loop but takes so much
I printed the result of site and actually it's correct: prints the tlds[i] between quotation marks but just takes so long.
In an an end-to-end test there were a lot of variables at play, a couple of examples can be Sauce startup, Browser start up and latency to and from the application under test, which may not be visible through the naked eye. Hence the delay.
You can find a couple of relevant detailed discussions in:
Why should I ever use CSS selectors as opposed to XPath for automated testing?
cssSelector vs XPath for selenium
I am writing a webscraper that uses data from a already existing spreadsheet to pull data from a website. It uses codes (that reference products) from a certain column to search the site. However, when searching for one product, multiple are displayed with only one being a correct match. I have created a system that can search for the correct code and select the product via find_element_by_xpath, but it does not account for multiple pages. My goal is to (upon the code not being found) move to the next page and search for the same code without moving to the next excel row, stopping when the final page is reached. I have already found a snippet of code that should work on moving to the next page:
try:
_driver.find_element_by_class_name("next").click()
print("Navigating to Next Page")
except TimeoutException as e:
print("Final Page")
break
However, I am unsure where/how I would implement this without either breaking the code, or moving down by a row.
Here is a snippet of how my code works so far (obviously simplified)
for i in data.index: #(_data is spreadsheet column)
try:
# locate product code
# copy product link
# navigate to link
try:
# wait for site to load
# Copy data to Spreadsheet
except TimeoutException:
# Skip if site takes too long
except Exception as e:
# Catch any possible exceptions and continues loop (normally when product cannot be found)
Any help would be much appreciated, whether it be how to implement the code snippet above, or a better way to go about moving from page to page. IF needed I can supply a link to the website or snippets of my code in further detail :)
A Python program terminates as soon as it encounters an error. In Python, an error can be a syntax error or an exception. The try-except code lets you test code and catch the exception that might occur without terminating the program.
To your question, you might want to use recursion functions in order to travel through the pages.
You could try something like this :
def rec(site, product):
if(final-page)
return exception_not_found
try:
# locate product code
try:
# wait for site to load
# Copy data to Spreadsheet
if(found_product)
return #found, break
except TimeoutException:
return # Skip if site takes too long
except Exception as e:
return # Skip if fails ?
if(we_did_not_find_product)
# copy product link
# navigate to link
#navigate to next site
rec(next_site, product)
for i in data.index: #(_data is spreadsheet column)
rec(init_side, i)
Meaning for each row in the spreadsheet, we go the the initial page, look for the product, if we did not find it, moving to next page until either we found the product or we reached the last page. Going to next row in cases : if exception occures, found the product, reached next page.
How I went about it (Storing the page mover and code checker as functions and using them to call each other):
def page_mover():
try:
# Click Next page
page_link()
except Exception:
print("Last page reached")
def page_link():
try:
# Wait for page to load
# Get link using product code
# Go to link
except Exception:
page_mover()
I'm trying to scrape every item on a site that's displayed in a grid format with infinite scrolling. However, I'm stuck on even getting the second item using xpath because it's saying:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='el-card advertisement card is-always-shadow'][2]"}
x = 1
while x < 5:
time.sleep(5)
target = driver.find_element_by_xpath(f"//div[#class='el-card advertisement card is-always-shadow'][{x}]")
target.click()
wait.until(EC.visibility_of_element_located((By.ID, "details")))
print(driver.current_url)
time.sleep(5)
driver.back()
time.sleep(5)
WebDriverWait(driver, 3).until(EC.title_contains("Just Sold"))
time.sleep(5)
x += 1
With my f-string xpath it's able to find the first div with that class and print the URL, but the moment it completes one iteration of the while loop, it fails to find the 2nd div with that class (so 2).
I've tried monitoring it with all the time.sleep() to see exactly where it was failing because I thought maybe it was running before the page loaded and therefore it couldn't be found, but I gave it ample time to finish loading every page and yet I can't find the issue.
This is the structure of the HTML on that page:
There is a class of that name (as well as "el-card__body" which I have also tried using) within each div, one for each item being displayed.
(This is what each div looks like)
Thank you for the help in advance!
(disclaimer: this is for a research paper, I do not plan on selling/benefiting off of the information being collected)
Storing each item in a list using find_elements_by_xpath, then iterating through them did the trick, as suggested by tdelaney.
So I have a selenium script that will automatically enter a series of numbers into a website, and the website will redirect the user to another website based off if the numbers match a PIN. However, the browser takes a short time to redirect the user, in which the next line of code would have already run and returned an error.
I was thinking something like this would work but it doesn't, I don't know why.
def checkElement():
try:
xpath = '//*[#id="name"]'
print("Page is ready!")
except TimeoutException:
print("failed")
checkElement()
I believe that you are looking for WebDriverWait. You can add specific condition in it. Please find the sample code below.
first_result = wait.until(presence_of_element_located((By.XPATH, "//*[#id='name']")))
i have written a sample code in selenium using python language and i encountered with some problem didn't get any solution
in the web, and my problem is in selenium i have kept one validation if object is available then go to next line other wise get out
from there, but selenium is keep on waiting for that object if the object is not present other wise it is returning true value and
executing fine..my requirement is if the object is not available get out from there and return false..
i have written code like this
Type:1:
try:
element = driver.find_element_by_id("DateOfBirthD")
elementfound=element.is_displayed()
if elementfound==True:
print "element found"
else:
print "element not found"
except:
print "no element"
Type:2:
try:
element = WebDriverWait(driver, 2000).until(EC.presence_of_element_located((By.ID, "DateOfBirthD")))
#do something
except:
print "element not found"
In the above two types if the element is found it is working fine and going to next line
but if the element is not found it is waiting for long time till the element is found as we know there is no element
please find me a solution to how to validate the objects in selenium
The amount of time that you want it to wait is specified in seconds. In your case, you are telling it to wait 2000 seconds before continuing.
You can read about it at the official docs: http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp