I am grabbing a list of buttons, then I am attempting to click a specific button object, I collect all buttons (which contain details such as a name). I check the name, if I have already clicked on this button, pass, or else click this button object.
The problem I am having is that the button doesn't have an ID so I am unsure of how to dynamically identify the button to click it (from the object in the loop). It doesn't have an HREF either.
If it had an ID, I could grab the ID value and build a selenium click even such as browser.find_element(By.ID, "button-ID").click()
.
Any pointers would be appreciated.
html = browser.page_source
soup = BeautifulSoup(html, "lxml")
buttons = soup.find_all('button', {'class', 'full-width'})
for button in buttons:
button_soup = BeautifulSoup(str(button), 'lxml')
name_div = button_soup.find('div', {'class': 'artdeco-entity-lockup__title'})
name = name_div.find('span', {'aria-hidden': 'true'}).text
if name in lead_check:
pass
else:
"""HERE IS WHERE I NEED TO CLICK THE BUTTON OBJECT"""
browser.find_element(By.CSS_SELECTOR, "button.full-width").click()
Button Object (one button from loop) example
<button role="button" type="button">
<div id="112">
<div id="113" type="circle">
<div >
<div >
<!-- --> <img alt="" height="48" id="114" loading="lazy" src="linktopic" width="48"/>
</div>
</div>
<!-- -->
</div>
<div id="115">
<div>
<div id="116">
<span dir="ltr"><span aria-hidden="true"><!-- -->John Smith<!-- --></span><span ><!-- -->View Johns’s profile<!-- --></span></span>
</div>
<div id="117"><!-- --> <span aria-hidden="false" >
· 3rd
</span>
<!-- --><!-- --></div>
<div id="118">
<!-- -->Loves cats<!-- -->
</div>
<!-- --> </div>
<!-- --> </div>
</div>
</button>
CodePudding user response:
I solved this by identifying a unique variable in each button, in this instance an IMG URL then dynamically located each with selenium.
img_src = button_soup.find('img')['src']
path_location = "//img[contains(@src,'{}')]".format(img_src)
browser.find_element(By.XPATH, path_location).click()