我是Selenium Python和JavaScript的新手。我正在尝试使用Selenium python自动化网页导航。我想单击一个Java脚本按钮。下面是html标签。
<button type="button" class="hpButton" onclick="loadAddPage()" id="btnNew" tabindex="100" style="width: 83px;">New...</button>
如何点击Selenium Python中的“新建...”按钮。我已经尝试了下面的事情,但是收到错误。
driver.find_element_by_id('btnNew')
和
NEXT_BUTTON_XPATH = '//input[@type="button" and @id="btnNew"]'
button = driver.find_element_by_xpath(NEXT_BUTTON_XPATH)
button.click()
请求的HTML片段
<script type='text/javascript' language='JavaScript'>
var FORM_URL_ID_MY_DISCOVERY_LIST = '/mxportal/settings/AutoDiscoveryList.jsp';</script>
<script type='text/javascript' language='JavaScript'>
window.SELECTABLE_ID_MY_DISCOVERY_LIST = true;window.PRESELECTED_ID_MY_DISCOVERY_LIST = true;window.PRESELECTION_DEFAULT_ID_MY_DISCOVERY_LIST = true;</script>
<div id='ID_MY_DISCOVERY_LIST_headerDiv' class='headerDivOuter'><div class='headerDivInner'><div onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=91787367&sortUp=true")' class='visualColumnHeader sortable'><div class='pad0x5'></div></div><div onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=0&sortUp=false")' class='visualColumnHeader sortedAscending'><div class='pad0x5'>Name</div></div><div onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=1&sortUp=true")' class='visualColumnHeader sortable'><div class='pad0x5'>Last Run</div></div><div onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=2&sortUp=true")' class='visualColumnHeader sortable'><div class='pad0x5'>Schedule</div></div><div onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=3&sortUp=true")' class='visualColumnHeader sortable'><div class='pad0x5'>Comments</div></div></div></div><div id='ID_MY_DISCOVERY_LIST_scrollingTableDiv' style='clear:both;overflow:visible'><table id='ID_MY_DISCOVERY_LIST' cellspacing='0' class='scrollingTable' cellpadding='0'><thead><tr class='readerCaptionRow'><th onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=91787367&sortUp=true")' class='checkboxCell sortable'></th><th onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=0&sortUp=false")' class='sortedAscending' scope='col'>Name</th><th onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=1&sortUp=true")' class='sortable' scope='col'>Last Run</th><th onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=2&sortUp=true")' class='sortable' scope='col'>Schedule</th><th onClick='MxFragmentLoad("/mxportal/settings/AutoDiscoveryList.jsp?MxTableSort=true&table=ID_MY_DISCOVERY_LIST&sortCol=3&sortUp=true")' class='sortable' scope='col'>Comments</th></tr></thead><tbody><tr id='ID_MY_DISCOVERY_LIST_dummyRow' class='dummyRow'><td><div></div></td><td class='sorted'><div></div></td><td><div></div></td><td><div></div></td><td><div></div></td></tr><tr class='altRowColor rowHighlight'><td scope='row' class='checkboxCell'><input rowselector='yes' checked name='ID_MY_DISCOVERY_LIST_selector' type='RADIO'></td><td class='sorted'>ESX VC</td><td>1/13/17 11:00 AM</td><td>Periodic - Next Run: 1/14/17 11:00 AM</td><td></td></tr><tr><td scope='row' class='checkboxCell'><input name='ID_MY_DISCOVERY_LIST_selector' type='RADIO' rowselector='yes'></td><td class='sorted'>Hyper-vHost</td><td>1/13/17 11:00 AM</td><td>Periodic - Next Run: 1/14/17 11:00 AM</td><td></td></tr><tr class='altRowColor'><td scope='row' class='checkboxCell'><input name='ID_MY_DISCOVERY_LIST_selector' type='RADIO' rowselector='yes'></td><td class='sorted'>OA</td><td>1/13/17 11:00 AM</td><td>Periodic - Next Run: 1/14/17 11:00 AM</td><td></td></tr><tr><td scope='row' class='checkboxCell'><input name='ID_MY_DISCOVERY_LIST_selector' type='RADIO' rowselector='yes'></td><td class='sorted'>OA 42.35</td><td>1/13/17 11:00 AM</td><td>Periodic - Next Run: 1/14/17 11:00 AM</td><td></td></tr><tr class='altRowColor'><td scope='row' class='checkboxCell'><input name='ID_MY_DISCOVERY_LIST_selector' type='RADIO' rowselector='yes'></td><td class='sorted'>System Automatic Discovery</td><td>Never</td><td><b>Task is Disabled</b> - Periodic</td><td></td></tr><tr style='display:none' class='noDataRow'><td colspan='5'>No discovery tasks defined.</td></tr></tbody></table></div>
<div style="margin-top:15px">
<div class='buttonSet'>
<div class='bWrapperUp'><div><div><button type='button' class='hpButton' onclick='runNowStop()' id='btnRunNowStop' tabindex='105'>Run Now</button></div></div></div>
<div class='bWrapperUp'><div><div><button type='button' class='hpButton' onclick='viewTaskResults()' id='btnViewTaskResults' tabindex='104'>View Task Results...</button></div></div></div>
<div class='bWrapperUp'><div><div><button type='button' class='hpButton' onclick='confirmDeleteDiscoveryTask()' id='btnDelete' tabindex='103'>Delete...</button></div></div></div>
<div class='bWrapperUp'><div><div><button type='button' class='hpButton' onclick='enableDisableDiscoveryTask()' id='btnEnableDisable' tabindex='102'>Disable</button></div></div></div>
<div class='bWrapperUp'><div><div><button type='button' class='hpButton' onclick='loadEditPage()' id='btnEdit' tabindex='101'>Edit...</button></div></div></div>
<div class='bWrapperUp'><div><div><button type='button' class='hpButton' onclick='loadAddPage()' id='btnNew' tabindex='100'>New...</button></div></div></div>
</div>
<div class='clearFloats'></div>
答案 0 :(得分:4)
我猜这是一个帧问题。
即使你有一个完全正确的XPATH, Selenium也不会看到该元素,除非你已经在包含该元素的框架中。
要快速查看您所在页面上有多少帧:
a = driver.find_elements_by_css_selector("frame")
print(len(a))
如果你发现有三个帧,你可以这样做:
driver.switch_to_frame(a[0])
button.click()
或
driver.switch_to_frame(a[1])
button.click()
或
driver.switch_to_frame(a[2])
button.click()
答案 1 :(得分:0)
您可以使用以下方法找到元素
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
请参阅http://selenium-python.readthedocs.io/locating-elements.html
上的文档同样的一些例子在
之下<强> 1。通过CSS选择器
driver.find_element_by_css_selector('.button.c_button.s_button').click()
<强> 2。按名称
driver.find_element_by_name('s_image').click()
第3。按ClassName
driver.find_element_by_class_name('s_image').click()
答案 2 :(得分:0)
您应该尝试在点击按钮之前添加一些等待时间,如下所示:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "btnNew")));
button.click();
使用上面的代码,您可以等待最多20秒,目标按钮变为可点击。否则,您将获得TimeOutException
如果此解决方案无效,请检查您的按钮是否位于<iframe>
元素内。如果是这样,请在单击按钮之前切换到该帧:
driver.switch_to_frame('iframe_id')