finding the XPath for button inside strong tag

Question:

I am attempting to find a robust xpath to click a button in the screenshot below

I want to click on the "eStatus" button

I want to click on the "eStatus" button

The HTML of the element is made up on the following

The HTML of the element is made up on the following

I have attempted to locate the button using the following xpath but it is not working.

login_Check=driver.find_element(By.XPATH,'/html/body/div/div/div[1]/ul/table/tbody/tr/td[2]/li/strong/em').click()

Please help me with this.

Asked By: Dev

||

Answers:

Try this:

Relative XPath expression: //strong//em[text()='eStatus']

login_Check = driver.find_element(By.XPATH, '//strong//em[text()="eStatus"]')
login_Check.click()

UPDATE: Try by inducing waits as below:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//strong//em[text()='eStatus']"))).click()

Import statements required:

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

How to check if XPath expression locates the desired element:

On your applicaiton, press F12 –> Ctrl+F –> Paste the XPath expression (In this case try //strong//em[text()='eStatus'] or this //li[@id='mn1'][strong/em[text()='eStatus']])

Answered By: Shawn

Use below code to locate the element using xpath:

driver.find_element(By.XPATH, "//li[@id='mn1'][strong/em[text()='eStatus']]")

Since, the button is inside frame element, use below code to switch to the frame before accessing the button using above xpath:

frame_element = driver.find_element_by_name("IDXHeader")
driver.switch_to.frame(frame_element)

Once you’re done accessing element inside frame, make sure you siwtch back to default content using below code:

driver.switch_to.default_content()

So, your code should be as below:

frame_element = driver.find_element(By.NAME, "IDXHeader")
driver.switch_to.frame(frame_element)
driver.find_element(By.XPATH, "//li[@id='mn1'][strong/em[text()='eStatus']]").click()
driver.switch_to.default_content()

You could also use below code to locate button:

driver.find_element(By.XPATH, "//li[strong/em[text()='eStatus']]")
Answered By: Harish
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.