Python Seleniume Multiple Buttons with same name
Question:
For the below HTML code, I want to select the button in the DIV for 2022 November
<div class="reportTitleNonMobile ng-binding">
2022 November
<button class="myButton" ng-show="!gridFile.monthOpen" ng-click="toggleMonthReport(gridFile)">Open</button>
<button class="myButton ng-hide" ng-show="gridFile.monthOpen" ng-click="toggleMonthReport(gridFile)">Close</button>
</div>
However the same is repeated for all the months.
Below is the link to the larger HTML Code for reference. Any help is appreciated:
I have tried the following:
driver.find_element_by_xpath(‘//*[@id="reportHeaderCol4NonMobile"]/div[2]/div[3]/div[3]/fieldset/div1/div1/div’).click()
driver.find_element_by_link_text(‘Open’)1.click()
driver.find_element_by_class_name(‘button.myButton’)1.click()
driver.find_element_by_class_name(‘myButton’)1.click()
and a bunch more but none of them seem to work and I end up with error Message: no such element: Unable to locate element
—- Edit / Addition—
I even tried the suggestion from @AbiSaran with Xpath as (.//*[@class=’reportTitleAreaNonMobile’])1//button1 but got the same error.
I feel the Xpath’s are correct since I am able to find them in Chrome > Inspect screen. But my python code fails.
———–Solution—
The solution below was pointing to accurate Xpath but was not working since the website was opening a new tab and the code continued to look for the element in the previous tab. I has the switch the active tab using the doe below and it worked fine.
driver.switch_to.window(driver.window_handles[1])
Answers:
You can use the below XPath:
For the ‘Open’ button in ‘2022 November’:
(.//*[@class='reportTitleAreaNonMobile'])[1]//button[1]
For the ‘Close’ button in ‘2022 November’:
(.//*[@class='reportTitleAreaNonMobile'])[1]//button[2]
For the below HTML code, I want to select the button in the DIV for 2022 November
<div class="reportTitleNonMobile ng-binding">
2022 November
<button class="myButton" ng-show="!gridFile.monthOpen" ng-click="toggleMonthReport(gridFile)">Open</button>
<button class="myButton ng-hide" ng-show="gridFile.monthOpen" ng-click="toggleMonthReport(gridFile)">Close</button>
</div>
However the same is repeated for all the months.
Below is the link to the larger HTML Code for reference. Any help is appreciated:
I have tried the following:
driver.find_element_by_xpath(‘//*[@id="reportHeaderCol4NonMobile"]/div[2]/div[3]/div[3]/fieldset/div1/div1/div’).click()
driver.find_element_by_link_text(‘Open’)1.click()
driver.find_element_by_class_name(‘button.myButton’)1.click()
driver.find_element_by_class_name(‘myButton’)1.click()
and a bunch more but none of them seem to work and I end up with error Message: no such element: Unable to locate element
—- Edit / Addition—
I even tried the suggestion from @AbiSaran with Xpath as (.//*[@class=’reportTitleAreaNonMobile’])1//button1 but got the same error.
I feel the Xpath’s are correct since I am able to find them in Chrome > Inspect screen. But my python code fails.
———–Solution—
The solution below was pointing to accurate Xpath but was not working since the website was opening a new tab and the code continued to look for the element in the previous tab. I has the switch the active tab using the doe below and it worked fine.
driver.switch_to.window(driver.window_handles[1])
You can use the below XPath:
For the ‘Open’ button in ‘2022 November’:
(.//*[@class='reportTitleAreaNonMobile'])[1]//button[1]
For the ‘Close’ button in ‘2022 November’:
(.//*[@class='reportTitleAreaNonMobile'])[1]//button[2]