Hiro 714
asked on
Python Selenium question
Please advise me how to append the data.
This doesn't append.
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:]
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_Titles.append(Ticket_Titles1)
Ticket_Discriptions.append(Ticket_Discriptions1)
Ticket_Status.append(Ticket_Status1)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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)
the number is good.44 44 44
but I don't knowfor Ticket_Title in Ticket_Titles:
print(Ticket_Title.text)
---------------------------------------------------------------------------
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
Check what's your stored Elements in your list after append the new list.
print(Ticket_Titles)
ASKER
Can you use the same method to print the list (before append any new item)?
ASKER
ASKER
By referring your existing code,it seems like you are building the nested dictionary instead?
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
Please advise which one is the latest version before i share my further comment?
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)
Refer: https://www.experts-exchange.com/questions/29219612/Python-Selenium-question.html#a43310038However, 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)
You latest sample showing error code when you trying to print out Ticket_Title.text below during iteration the listfor Ticket_Title in Ticket_Titles:
print(Ticket_Title.text)
Please advise which one is the latest version before i share my further comment?
ASKER
i want to use pandas. that's my final goal
ASKER
It works before appending.
ASKER
after append, i get this error.
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)
ASKER
thank you, but getting this error.
ASKER
Does "extend" append a whole text?
Can "extend" append following contents?
Can "extend" append following contents?
i have stimulated the sample data, it is working fine here.
check if you can output all the Ticket items correctly
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)
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>")
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.
ASKER
thank you, but getting this error.
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)
ASKER
is this driver issue?
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?
Show where the list stored the expected results?
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:]
Show where the list stored the expected results?
print(Ticket_Titles)
print(Ticket_Discriptions)
print(Ticket_Status)
ASKER
print(Ticket_Titles)
print(Ticket_Discriptions)
print(Ticket_Status)
should beTicket_Titles.text = ['1234','5678','9012']
Ticket_Discriptions.text = ['abcd','efgh','abab']
Ticket_Status.text = ['pending','pending','resolved']
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)
ORimport pandas as pd
d = {'Number':Ticket_Titles.text,'Titles':Ticket_Discriptions.text,'Status':Ticket_Status.text}
df = pd.DataFrame(d)
print(df.to_string())
ASKER
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.
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
Please share more information whether the given attempt resolve your issue?
https://www.experts-exchange.com/questions/29219612/Python-Selenium-question.html#a43311025
ASKER
I'm getting this error now.
Open in new window
Open in new window