How to derive xpath for the following highlighted element?as I am seeing all the class names are same. I have tried using parent child traverse also but it did not work as all the 4 child elemetns are having same attribute. I cant use "Samsung - S10" text because its a variable and will change for respective devices.
CodePudding user response:
As a last resort, I always use xpath indexing. I know it's not the best practice but it always work for me.
You can try:
(//dd[@class='m-0 text-base'])[1]
should represent the first node, you can change to [2], [3] to locate the 2nd and 3rd node respectively.
Also, not sure if you can use Device Protected in your xpath, if you could then we'd have to use that and we can eliminate the xpath indexing.
CodePudding user response:
I think, for your highlighted text, you can use the following xpath :
//div[@class='mb-3' and contains(@style, 'display: grid;')]//dl//dt[contains(text(), 'DEVICE PROTECTED')]/following-sibling::dd
From there, you can insert this xpath into your driver and then get the elements into a list
my_xpath = "//div[@class='mb-3' and contains(@style, 'display: grid;')]//dl//dt[contains(text(), 'DEVICE PROTECTED')]/following-sibling::dd"
myElements = driver.find_elements(By.XPATH, my_xpath)
Once you get your elements list, you can get the text of each element and do whatever you feel like with it
my_xpath = "//div[@class='mb-3' and contains(@style, 'display: grid;')]//dl//dt[contains(text(), 'DEVICE PROTECTED')]/following-sibling::dd"
my_list_length = driver.find_elements(By.XPATH, my_xpath).__len__()
for rec_num in range(my_list_length):
newXPath = "{0}[{1}]".format(my_xpath, f'{rec_num 1}')
ourElement = driver.find_element(By.XPATH, newXPath)
print(f'new element text: {ourElement.text}')
If there is, only, 1 record that displays and you just want the device name, then you could do this as well:
my_xpath = "//div[@class='mb-3' and contains(@style, 'display: grid;')]//dl//dt[contains(text(), 'DEVICE PROTECTED')]/following-sibling::dd"
myText = driver.find_element(By.XPATH, my_xpath).text

