代码之家  ›  专栏  ›  技术社区  ›  Kamikaze_goldfish

为什么selenium不在python中单击按钮?

  •  0
  • Kamikaze_goldfish  · 技术社区  · 7 年前

    我对按钮本身使用xpath选择器,selenium给了我一个错误 selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="ember1355"]/button"} 当我试着点击的时候。我要做的是使用selenium与linkedin中的人建立联系。当我搜索一个特定的词,然后我想拉出所有的配置文件说“连接”,并点击每个按钮。html是

    <div class="search-result__actions">
            <div id="ember3093" class="ember-view">    
                <button aria-label="Connect with Persons name" class="search-result__actions--primary button-secondary-medium m5" data-control-name="srp_profile_actions" data-ember-action="" data-ember-action-3096="3096">Connect</button>   
            <div id="ember3099" class="ember-view"><div id="ember3100" class="ember-view"><!----></div></div>
            </div>
            </div>
    

    我的代码是:

    if each.find('div', class_='search-result__actions').find('div').text.strip() == "Connect":
        id = each.find('div', class_='search-result__actions').find('div')['id']
        id = '//*[@id="'+id+'"]'
        print(id)
        #this is where the code isn't working
        driver.find_element_by_xpath('//*[@id="ember1355"]/button').click()
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Kamikaze_goldfish    7 年前

    Xpath

    尝试 css\U选择器 :

    # Finds the right css selector
    button = driver.find_element_by_css_selector('.search-result__actions--primary')
    # Clicks the button
    driver.execute_script("arguments[0].click();", button)