title: Conditional selection of elements in Selenium webdriver in Python
body: <p>I'm facing a situation where a modal sometimes has a desired button for logging into Facebook and sometimes I have to click 'More Options' to get to the Facebook button. Another hurdle is that the xpath of the <code>more_options_btn</code> is the same as one called 'Trouble logging in?' so I added a <code>[contains(text(),"More Options")]</code>, but am unsure of the syntax here and cannot find the right approach in the docs. In any case it is throwing an error, which might be caused by the fact that the <code>button</code> element does not have text, instead it is nested like this <code>button &gt; span &gt; text</code></p>
  
  <p>Please keep in mind that I want to make my solution as dynamic and robust as possible, but don't have a lot of experience with Selenium yet.</p>
  
  <pre class="lang-py prettyprint-override"><code>from selenium import webdriver
  from webdriver_manager.chrome import ChromeDriverManager
  
  driver = webdriver.Chrome(ChromeDriverManager().install())
  
  driver.get('http://localhost:3000/')
  
  try:
      fb_btn = driver.find_element_by_xpath(
          '//*[@id="modal-manager"]/div/div/div/div/div[3]/span/div[2]/button')
      fb_btn.click()
  except:
      more_options_btn = driver.find_element_by_xpath(
          '//*[@id="modal-manager"]/div/div/div/div/div[3]/span/button[contains(text(),"More Options")]')
      more_options_btn.click()
  
      fb_btn = driver.find_element_by_xpath(
          '//*[@id="modal-manager"]/div/div/div/div/div[3]/span/div[2]/button')
      fb_btn.click()
  
  </code></pre>