Exercise webtables: I have used the same code but still getting error | Selenium Python Forum
N
Nitika Posted on 06/11/2020
 

Code>

'''
Created on 05-May-2020
@author: jaspreet
'''
from selenium import webdriver
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

name = "Jenette Caldwell" # present on page 6 row no. 7
rowNum = -1


# common utility functions
def elementPresent(locator, locatorType):
# present : true
# not present : false
wait = WebDriverWait(driver, 10)
try:
wait.until(EC.presence_of_all_elements_located((locatorType, locator)))
wait.until(EC.visibility_of_all_elements_located((locatorType, locator)))
except Exception:
return False
return True


def getRowNumByName(name):
a = driver.find_elements_by_xpath("//*[@id='dtBasicExample']/tbody/tr")
for i in range(0, len(a)):
row = a[i]
column = row.find_elements_by_tag_name("td")
for j in range(0, len(column)):
if ((column[j].text) == name):
return i + 1
return -1


# main script
options = webdriver.ChromeOptions()
options.add_argument("--disable-infobars")
options.add_argument("--disable-notifications")
options.add_argument("--disable-bookmarks")
options.add_argument("--start-maximized")

driver = webdriver.Chrome(options=options)
driver.implicitly_wait(10)
driver.get("https://mdbootstrap.com/docs/jquery/tables/pagination/")

page = 1
rowNum = getRowNumByName(name)
while (rowNum == -1):
if (elementPresent("//li[@class='paginate_button page-item next']/a", "xpath")):
driver.find_element_by_xpath("//li[@class='paginate_button page-item next']/a").click();
page = page + 1
rowNum = getRowNumByName(name)
else:
print("Name not found")

if (rowNum != -1):
print("Given name '" + name + "'" + " found")
print("Page number - " + str(page))
cellData = driver.find_element_by_xpath("//*[@id='dtBasicExample']/tbody/tr[" + str(rowNum) + "]/td[3]")
# print("Age is : "+age.text)
print(cellData.text)
time.sleep(5)
driver.quit()



Error>

Traceback (most recent call last):
File "C:/Users/Dell/PycharmProjects/whizdom/zohoo/webtableExer2.py", line 50, in <module>
rowNum = getRowNumByName(name)
File "C:/Users/Dell/PycharmProjects/whizdom/zohoo/webtableExer2.py", line 33, in getRowNumByName
if ((column[j].text) == name):
File "C:\Users\Dell\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webelement.py", line 76, in text
return self._execute(Command.GET_ELEMENT_TEXT)['value']
File "C:\Users\Dell\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\Dell\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Dell\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=86.0.4240.183)


Process finished with exit code 1

 

 

Hello,

I have used the same code but still I am  facing this issue again n again.Please help


0
09914040666 Replied on 07/11/2020

Hey

The URL used is taking time to load. Kindy put implicitly wait below driver.get(). The code will work fine.


N
Nitika Replied on 07/11/2020

Implicitly wait is already there above driver.get can you please check again

One more query can we use implicit wait n explicit wait together as well?


0
09914040666 Replied on 12/11/2020

Hey

The URL used in the example for dynamic table is slow, it takes time to load. The reason the error is coming just because the element is not being found due to slow loading page. You may introduce time.sleep() after after giving the command driver.get().

Regarding the second query, yes we can use implicitly wait and explicit wait together. Implicit wait is the waiting technique which is global for all driver.find commands and explicit wait is the technique to wait for a element on some condition.


Related Posts