rogertn
rogertn

Reputation: 1

Clicking a button with a conditional Selenium

I'm a beginner, learning selenium packpage using python language.

How could I click on the button with the text: "Co-assinar" only when there is the text: 'Segue a certidão..."

I'll put an image below the part of the code I'm talking about.

https://i.sstatic.net/8jV64.png

Upvotes: 0

Views: 125

Answers (2)

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

try:
   driver.find_element(By.XPATH,"//p[contains(text(),'Segue a certidão')]")
   driver.find_element(By.XPATH,"//a[./i[text()=' Co assinar ']]").click()
except:
   pass

You can check for the second element and then click the first element if it's there. Handling any exceptions you may want to in case.

Upvotes: 0

Connor Dudley
Connor Dudley

Reputation: 56

Your best bet is to "collect" the two elements you'd like as variables and then use an if statement to check and see if the elements text is equal to "Segue a..." and in that case click.

Something like this:

#Get the button you want to click
button_to_click = driver.find_element(By.CLASS_NAME, 'menor link_amissao_assinar...')

#Get the element that holds the text you're checking
text_you_want = driver.find_element(By.CLASS_NAME, 'texto_original').children[2].text

#If element text equals 'Segue a...' click
if text_you_want == 'Segue a...':
    button_to_click.click()

Upvotes: 1

Related Questions