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:
customer_or_prize_list=driver.find_elements_by_xpath('//*[@id="customer_info" or @class="prize_info"]')
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()
:
for el in customer_or_prize_list: is_customer = "customer_info" in el.get_attribute("id") is_prize = "prize_info" in el.get_attribute("class")
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:
customer_list=driver.find_elements_by_xpath('//*[@id="customer_info"]') for e in customer_list: '''do multi_line_text_formatting''' prize_list=driver.find_elements_by_xpath('//*[@class="prize_info"]') for e in prize_list: '''do single_line_text_formatting'''