title: Python code to click a button using css selectors
body: <p>I am brand new to python and am trying to modify some existing code. I have a form with the following elements:</p>
  
  <pre><code>&lt;div class="v-actions"&gt;
      &lt;input type="submit" value="Save"/&gt;
      &lt;input type="button" value="Accept"/&gt;
      &lt;input type="button" value="Reject"/&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>If it exists, I want to click the <code>Accept</code> button. If not, I want to log that it is not found. Currently, the code is this:</p>
  
  <pre><code>from bs4 import BeautifulSoup
  import datetime
  import getpass
  from gmail import Gmail
  from selenium import webdriver
  from selenium.common.exceptions import NoSuchElementException
  from time import sleep
  
  ....
  
  self.chrome_session = webdriver.Chrome()
  
  ....
  
  links = self.chrome_session.find_elements_by_class_name('link-record')
  links = [(link.text, link.get_attribute('href').decode('utf-8'))
           for link in links]
  if len(links) == 0:
      print("No work orders available at {0}".format(
          datetime.datetime.now()))
  else:
      for link_text, link_url in links:
          print("Clicking work order {0}".format(link_text))
          self.chrome_session.get(link_url)
          potential_input = self.chrome_session.find_element_by_class_name('v-actions').find_element_by_tag_name('input');
          if potential_input.get_attribute('value') == 'Accept':
              potential_input.click()
              print("Accepted work order {0} at {1}.".format(link_text,datetime.datetime.now()))
          else:
              print("Accept input not found.")
  ....
  </code></pre>
  
  <p>In jquery, the selector would be something like:</p>
  
  <pre><code>div.v-actions input[value=Accept]
  </code></pre>
  
  <p>I am not sure how to do this with Python. I would rather not loop through all the inputs. I just want to find if the specific input exists and accept it if it does.</p>
  
  <p>How do I translate the css selector to Python?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I am trying this now:</p>
  
  <pre><code>potential_input = self.chrome_session.find_element_by_xpath("//div[@class='v-actions']/input[@value='Accept']")
  if potential_input.is_displayed():
      print("Accept input not found")
  else:
      potential_input.click()
      print("Accepted work order {0} at {1}.".format(link_text,
                                                     datetime.datetime.now()))
  </code></pre>
  
  <p>I am not sure how to test if it found the element. I get this error:</p>
  
  <pre><code>selenium.common.exceptions.ElementNotVisibleException: Message: {"errorMessage":"Element is not currently visible and may not be manipulated","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"81","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:49795","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"sessionId\": \"7b58b250-ca34-11e7-967d-6fd95b435952\", \"id\": \":wdc:1510771259080\"}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/7b58b250-ca34-11e7-967d-6fd95b435952/element/:wdc:1510771259080/click"}}
  </code></pre>
  
  <p>I am not sure if my selector is wrong, or if my test to see if it was found is bad.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>I have modified the code to the following:</p>
  
  <pre><code>for link_text, link_url in links:
      print("Clicking work order {0}".format(link_text))
      self.chrome_session.get(link_url)
      try:
          self.chrome_session.find_element_by_xpath("//input[@value='Accept']").click()
          print("Accepted work order {0} at {1}.".format(link_text,datetime.datetime.now()))
      except ElementNotVisibleException:
          print("Accept input not found")
  self.chrome_session.back()
  </code></pre>
  
  <p>Now the code executes, and it acts like it worked (it prints <code>Accepted work order {0}</code>), but it didn't correctly fire the <code>.click()</code> because the record still exists in the list. But, if I manually click accept, it correctly processes the job.</p>
  
  <p>Is my selector still wrong? Or, is the click function incorrectly applied?</p>