Link to home
Start Free TrialLog in
Avatar of Hiro 714
Hiro 714

asked on

Python Selenium question

Please advise me how to append the data.
This doesn't append.

Ticket_Titles = driver.find_elements_by_xpath('//tbody/tr/td[2]/a')
Ticket_Discriptions = driver.find_elements_by_xpath('//tbody/tr/td[3]/a')
Ticket_Status = driver.find_elements_by_xpath('//tbody/tr/td[4]/a')
Ticket_Titles = Ticket_Titles[4:]

Open in new window


Ticket_Titles1 = driver.find_elements_by_xpath('//tbody/tr/td[2]/a')
Ticket_Discriptions1 = driver.find_elements_by_xpath('//tbody/tr/td[3]/a')
Ticket_Status1 = driver.find_elements_by_xpath('//tbody/tr/td[4]/a')

Open in new window

Ticket_Titles.append(Ticket_Titles1)
Ticket_Discriptions.append(Ticket_Discriptions1)
Ticket_Status.append(Ticket_Status1)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Hiro 714
Hiro 714

ASKER

Thank you.
I'm getting this error now.
Ticket_List = []
for i in range(len(Ticket_Titles)):
    temporary_data={'Number': Ticket_Titles[i].text,
                   'Titles': Ticket_Discriptions[i].text,
                   'Status': Ticket_Status[i].text
                   }
    Ticket_List.append(temporary_data)

Open in new window




---------------------------------------------------------------------------
StaleElementReferenceException            Traceback (most recent call last)
<ipython-input-386-25aa1d44f1ed> in <module>
      1 Ticket_List = []
      2 for i in range(len(Ticket_Titles)):
----> 3     temporary_data={'Number': Ticket_Titles[i].text,
      4                    'Titles': Ticket_Discriptions[i].text,
      5                    'Status': Ticket_Status[i].text 
~\AppData\Roaming\Python\Python38\site-packages\selenium\webdriver\remote\webelement.py in text(self)
     74     def text(self):
     75         """The text of the element."""
---> 76         return self._execute(Command.GET_ELEMENT_TEXT)['value']
     77
     78     def click(self):

~\AppData\Roaming\Python\Python38\site-packages\selenium\webdriver\remote\webelement.py in _execute(self, command, params)
    631             params = {}
    632         params['id'] = self._id
--> 633         return self._parent.execute(command, params)
    634
    635     def find_element(self, by=By.ID, value=None):

~\AppData\Roaming\Python\Python38\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None)) 
~\AppData\Roaming\Python\Python38\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243
    244     def _value_or_default(self, obj, key, default):

StaleElementReferenceException: Message: The element reference of <a href="/0576025009"> is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed 

Open in new window

Ticket_Titles1 = driver.find_elements_by_xpath('//tbody/tr/td[2]/a')
Ticket_Discriptions1 = driver.find_elements_by_xpath('//tbody/tr/td[3]/a')
Ticket_Status1 = driver.find_elements_by_xpath('//tbody/tr/td[4]/a')

Ticket_Titles1 = Ticket_Titles1[4:]

Ticket_Titles.extend(Ticket_Titles1)
Ticket_Discriptions.extend(Ticket_Discriptions1)
Ticket_Status.extend(Ticket_Status1)

Ticket1 = len(Ticket_Titles)
print(Ticket1)
Ticket2 = len(Ticket_Discriptions)
print(Ticket2)
Ticket3 = len(Ticket_Status)
print(Ticket3)

Open in new window

the number is good.

44 44 44

Open in new window

but I don't know
for Ticket_Title in Ticket_Titles:
    print(Ticket_Title.text)

Open in new window



---------------------------------------------------------------------------
StaleElementReferenceException            Traceback (most recent call last)
<ipython-input-387-fd91266a0092> in <module>
      1 for Ticket_Title in Ticket_Titles:
----> 2     print(Ticket_Title.text)

~\AppData\Roaming\Python\Python38\site-packages\selenium\webdriver\remote\webelement.py in text(self)
     74     def text(self):
     75         """The text of the element."""
---> 76         return self._execute(Command.GET_ELEMENT_TEXT)['value']
     77
     78     def click(self):

~\AppData\Roaming\Python\Python38\site-packages\selenium\webdriver\remote\webelement.py in _execute(self, command, params)
    631             params = {}
    632         params['id'] = self._id
--> 633         return self._parent.execute(command, params)
    634
    635     def find_element(self, by=By.ID, value=None):

~\AppData\Roaming\Python\Python38\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None)) 
~\AppData\Roaming\Python\Python38\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243
    244     def _value_or_default(self, obj, key, default):

StaleElementReferenceException: Message: The element reference of <a href="/0576025009"> is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed 

Open in new window

Check what's your stored Elements in your list after append the new list.

print(Ticket_Titles)

Open in new window

User generated imageUser generated image
Can you use the same method to print the list (before append any new item)?
User generated imageUser generated image
User generated image
By referring your existing code,it seems like you are building the nested dictionary instead?
Ticket_List = []
for i in range(len(Ticket_Titles)):
    temporary_data={'Number': Ticket_Titles[i].text,
                   'Titles': Ticket_Discriptions[i].text,
                   'Status': Ticket_Status[i].text
                   }
    Ticket_List.append(temporary_data)

Open in new window

Refer: https://www.experts-exchange.com/questions/29219612/Python-Selenium-question.html#a43310038

However, based on the current sample code that we're discussing showing error when you are showing the output before appending the item to the list ...

Refer: https://www.experts-exchange.com/questions/29219612/Python-Selenium-question.html#a43310039 
Ticket_Titles1 = driver.find_elements_by_xpath('//tbody/tr/td[2]/a')
Ticket_Discriptions1 = driver.find_elements_by_xpath('//tbody/tr/td[3]/a')
Ticket_Status1 = driver.find_elements_by_xpath('//tbody/tr/td[4]/a')

Ticket_Titles1 = Ticket_Titles1[4:]

Ticket_Titles.extend(Ticket_Titles1)
Ticket_Discriptions.extend(Ticket_Discriptions1)
Ticket_Status.extend(Ticket_Status1)

Ticket1 = len(Ticket_Titles)
print(Ticket1)
Ticket2 = len(Ticket_Discriptions)
print(Ticket2)
Ticket3 = len(Ticket_Status)
print(Ticket3)

Open in new window

You latest sample showing error code when you trying to print out Ticket_Title.text below during iteration the list
for Ticket_Title in Ticket_Titles:     
  print(Ticket_Title.text)

Open in new window


Please advise which one is the latest version before i share my further comment?
i want to use pandas. that's my final goal
It works before appending.
User generated image
after append, i get this error.
User generated image
Since you are using dictionary format, add "from_dict" to display the Ticket_List in pandas way.

Ticket_List = []
for i in range(len(Ticket_Titles)):
    temporary_data={'Number': Ticket_Titles[i].text,
                   'Titles': Ticket_Discriptions[i].text,
                   'Status': Ticket_Status[i].text
                   }
    Ticket_List.append(temporary_data)
   
df_data = pd.DataFrame.from_dict(Ticket_List)
df_data.dtypes
print(df_data)

Open in new window

thank you, but getting this error.
User generated image
Does "extend" append a whole text?
Can "extend" append following contents?
User generated imageUser generated image
i have stimulated the sample data, it is working fine here.

import pandas as pd
    
Ticket_List = [{'Number': 'A1', 'Titles': 'Title 1', 'Status': 1},
         {'Number': 'A2',  'Titles': 'Title 2', 'Status': 0},
         {'Number': 'A3',  'Titles': 'Title 3',  'Status': 1}]
  
df_data = pd.DataFrame.from_dict(Ticket_List)
print(df_data)

Open in new window

User generated image

check if you can output all the Ticket items correctly
for i in range(len(Ticket_Titles)):
   print(Ticket_Titles[i].text+Ticket_Discriptions[i].text+Ticket_Status[i].text+"<br>")

Open in new window


Does "extend" append a whole text? Can "extend" append following contents? 
You can't extend the mentioned text content to this dictionary list due to different formats. 
thank you, but getting this error.

User generated image
print your variable separately and check which item caused the error?

for i in range(len(Ticket_Titles)):
  print(Ticket_Titles[i].text)
  print(Ticket_Discriptions[i].text)
  print(Ticket_Status[i].text)

Open in new window

is this driver issue?
User generated image
Can you share your actual code how you scrap the data for the defined lists?

Is this the correct list that you are showing here?
Ticket_Titles = driver.find_elements_by_xpath('//tbody/tr/td[2]/a')
Ticket_Discriptions = driver.find_elements_by_xpath('//tbody/tr/td[3]/a')
Ticket_Status = driver.find_elements_by_xpath('//tbody/tr/td[4]/a')
Ticket_Titles = Ticket_Titles[4:]

Open in new window


Show where the list stored the expected results?
print(Ticket_Titles)
print(Ticket_Discriptions)
print(Ticket_Status)

Open in new window

print(Ticket_Titles)
print(Ticket_Discriptions)
print(Ticket_Status)

Open in new window

should be
Ticket_Titles.text = ['1234','5678','9012']
Ticket_Discriptions.text = ['abcd','efgh','abab']
Ticket_Status.text = ['pending','pending','resolved']

Open in new window

Let me try to put confidential data hidden and upload source code
If that is the case...try this based on your returned list as given (https://www.experts-exchange.com/questions/29219612/Python-Selenium-question.html#a43310997 ):

import pandas as pd

for i in range(len(Ticket_Titles.text)):
    temporary_data={'Number': Ticket_Titles.text[i],
                   'Titles': Ticket_Discriptions.text[i],
                   'Status': Ticket_Status.text[i]
                   }
    Ticket_List.append(temporary_data)

df_data = pd.DataFrame.from_dict(Ticket_List)
print(df_data)

Open in new window

OR
import pandas as pd

d = {'Number':Ticket_Titles.text,'Titles':Ticket_Discriptions.text,'Status':Ticket_Status.text}
df = pd.DataFrame(d)
print(df.to_string())

Open in new window

could i use extend fuction to append selenium element data?
after using extend, i can't use print function. wondering if the data structure does not work with selenium element.
Depend how you apply this extend function to your dictionary item. If you just want to extend to normal list, you can append any selenium element data.

Please share more information whether the given attempt resolve your issue?
https://www.experts-exchange.com/questions/29219612/Python-Selenium-question.html#a43311025