【发布时间】:2018-10-02 00:41:56
【问题描述】:
我正在为按钮本身使用 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()
【问题讨论】:
-
要选择的元素不在视野范围内。我建议在尝试点击之前滚动到元素。
-
是否可以滚动到该特定元素?