Selenium (Python) clicking an element within a frame

I'm having an issue within LinkedIn. Once you click a "Connect" button, a frame comes up that looks like this: enter image description here

I'm trying to click the "Send now" button, which says it's not visible. I've tried to switch frames by every known id/class. The error says that switching frames cannot be satisfied because the frame cannot be found. Anyone know what I'm doing wrong?

  <div id="li-modal-container"><div id="ember1954" class="modal-wormhole visible send-invite ember-view"><div aria-labelledby="ember1483-modal-label" role="dialog" tabindex="-1" class="modal-wormhole-content">
  <div class="modal-content-wrapper">
            <section class="modal">
    <div role="document">
      <header class="send-invite__header">
        <h2 id="ember1483-modal-description" class="Sans-21px-black-85% pv3">
            You can customize this invitation
          <button type="button" name="cancel" class="send-invite__cancel-btn" data-ember-action="" data-ember-action-1955="1955">
            <span class="svg-icon-wrap"><span class="visually-hidden">Close</span><li-icon aria-hidden="true" type="cancel-icon"><svg viewBox="0 0 24 24" width="24px" height="24px" x="0" y="0" preserveAspectRatio="xMinYMin meet" class="artdeco-icon"><g class="large-icon" style="fill: currentColor">
        <path d="M20,5.32L13.32,12,20,18.68,18.66,20,12,13.33,5.34,20,4,18.68,10.68,12,4,5.32,5.32,4,12,10.69,18.68,4Z"></path>

      <div class="ph4">
          <p id="ember1483-modal-label" class="pv4 mb3 display-flex">
              LinkedIn members are more likely to accept invitations that include a personal note.
<!---->      </div>

      <div class="send-invite__actions">
          <button class="button-secondary-large mr1" data-ember-action="" data-ember-action-1956="1956">
            Add a note
          <button class="button-primary-large ml1" data-ember-action="" data-ember-action-1957="1957">
              Send now

I've tried:

WebDriverWait(driver, 15).until(EC.visibility_of_element_located(By.XPATH("//butto‌​n[@class='button-pri‌​mary-large ml1']"))) 

and get this error: TypeError: 'str' object is not callable

1 answer

  • answered 2018-01-11 22:34 Bryan Oakley

    At least part of the problem is that you are not properly waiting for the element to be visible before trying to click on it.

    The code that you're trying to use is incorrect. Your question shows this code:

            By.XPATH("//butto‌​n[@class='button-pri‌​mary-large ml1']"))) 

    By.XPATH is not a function, it is a string constant. When you do By.XPATH("...") you are trying to call By.XPATH as if it were a function. This is why you get the error TypeError: 'str' object is not callable.

    The argument to visibility_of_element_located needs to be a tuple, where the first value is By.XPATH and the second value is the xpath.

    For example:

            (By.XPATH, "//butto‌​n[@class='button-pri‌​mary-large ml1']"))) 

    If the element is created dynamically, you may need to first wait for the element to exist before you can wait for the element to be visible. You can do that with EC.presence_of_element_located.