Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

I am trying to click a tab called source control, that tab has an id which is dynamically generated.on looking online, tried below methods but still no use

Tried the below XPath:

//li[@class="menu-item"]/a/strong[text(),Source Control]')

//li[@class="menu-item"]//a//text()[preceding-sibling::strong][normalize-space()!='']

//li//a[starts-with(id,"aui-uid-")]/strong[text(),Source Control]

Code that I am using

Sourcecontrol=driver.find_element_by_xpath('//li[@class="menu-item"]/a/strong[text(),Source Control]')

if not Sourcecontrol:

    print("No element found")  

else:

    Sourcecontrol.click();

HTML

<li class="menu-item" role="presentation">

                    <a href="link" id="aui-uid-4" role="tab" aria-selected="false"><strong>Source Control</strong></a>

                </li>

1 Answer

0 votes
by (62.9k points)

Use the subsequent XPath. It will search the substring within the anchor element. Try using either of the following choices

 try:

Sourcecontrol=driver.find_element_by_xpath('//li[@class="menu-item"]/a[contains(.,"Source Control")]')

Sourcecontrol.click();

except:

print("No element found")

Or

 if len(driver.find_elements_by_xpath('//li[@class="menu-item"]/a[contains(.,"Source Control")]'))>0:

Sourcecontrol = driver.find_element_by_xpath('//li[@class="menu-item"]/a[contains(.,"Source Control")]')

Sourcecontrol.click();

else:

print("No element found")

Browse Categories

...