I am using selenium in python with chrome driver.
Here is my scenario:
From a web page I extract all elements having id==customer_info
or class==prize_info
using the following code:
JavaScript
x
2
1
customer_or_prize_list=driver.find_elements_by_xpath('//*[@id="customer_info" or @class="prize_info"]')
2
Now I want to go through each element of the list ‘customer_or_prize_list’ and process as follows
- if web element contains
id==customer_info
then do multi_line_text_formatting (assume it as action 1) - if web element contains
class==prize_info
then do single_line_text_formatting (assume it as action 2)
You can assume that a web element will not contain both id and class.
How can I process list elements using 1. and 2. above?
Advertisement
Answer
When you go through each element of the list, get the attribute of the element using get_attribute()
:
JavaScript
1
4
1
for el in customer_or_prize_list:
2
is_customer = "customer_info" in el.get_attribute("id")
3
is_prize = "prize_info" in el.get_attribute("class")
4
See this url, if you want to know more about get_attribute()
.
Another way,
why don’t you divide the list into two small lists, like below:
JavaScript
1
8
1
customer_list=driver.find_elements_by_xpath('//*[@id="customer_info"]')
2
for e in customer_list:
3
'''do multi_line_text_formatting'''
4
5
prize_list=driver.find_elements_by_xpath('//*[@class="prize_info"]')
6
for e in prize_list:
7
'''do single_line_text_formatting'''
8