Home > database >  Clicking a button with a conditional Selenium
Clicking a button with a conditional Selenium

Time:06-17

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.stack.imgur.com/8jV64.png

CodePudding user response:

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()

CodePudding user response:

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.

  • Related