Selenium(Python)单击框架内的元素

时间:2018-01-11 21:14:40

标签: python selenium linkedin frame

我在LinkedIn内遇到问题。单击“连接”按钮后,会出现一个如下所示的框架: enter image description here

我正在尝试点击“立即发送”按钮,该按钮表示它不可见。我试图按每个已知的id / class切换帧。该错误表示无法满足切换帧,因为无法找到帧。谁知道我做错了什么?

  <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
                  </h2>
          <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>
      </g></svg></li-icon></span>
          </button>
      </header>

      <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.
                      </p>
<!---->      </div>

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

我试过了:

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

并收到此错误: TypeError:'str'对象不可调用

2 个答案:

答案 0 :(得分:0)

问题的至少一部分是你在尝试点击它之前没有正确等待元素可见。

您尝试使用的代码不正确。您的问题显示此代码:

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

By.XPATH不是函数,它是一个字符串常量。当您执行By.XPATH("...")时,您试图将By.XPATH称为函数。这就是您收到错误TypeError: 'str' object is not callable的原因。

visibility_of_element_located的参数需要是一个元组,其中第一个值为By.XPATH,第二个值为xpath。

例如:

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

如果元素是动态创建的,您可能需要先等待元素存在,然后才能等待元素可见。您可以使用EC.presence_of_element_located执行此操作。

答案 1 :(得分:0)

def connect_me():
sendNow = driver.find_element_by_css_selector(".button-primary-large.ml1")
driver.execute_script("arguments[0].style.visibility = 'visible'; arguments[0].click();", sendNow)
time.sleep(4)
return

这就是我能够点击它的方式。