不知道为什么,但是也许是像JS或隐藏元素之类的东西。
我需要点击“确认”按钮,但由于某种原因其不可点击。
此按钮位于一个不同的“块”中,与我填写的其他按钮不同。
此按钮的html
<input type="submit" name="ctl00$WpModaisCrm1$ctl03$btnRegistrarProtocolo" value="Confirmar" id="btnRegistrarProtocolo" class="btn btn-primary pull-right" autocomplete="off">
“取消”按钮的html是:
<input type="submit" name="ctl00$WpModaisCrm1$ctl03$btnCancelarProtocolo" value="Cancelar" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$WpModaisCrm1$ctl03$btnCancelarProtocolo", "", true, "", "", false, false))" id="ctl00_WpModaisCrm1_ctl03_btnCancelarProtocolo" class="btn btn-primary button-padding" data-dismiss="modal" autocomplete="off">
并且此HTML中有一个隐藏元素
<input type="submit" name="ctl00$WpModaisCrm1$ctl03$btnCarregarUcProtocoloAtendimento" value="" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$WpModaisCrm1$ctl03$btnCarregarUcProtocoloAtendimento", "", true, "", "", false, false))" id="btnCarregarUcProtocoloAtendimento" style="display: none" autocomplete="off">
代码:
confirmar = driver.find_element_by_id("btnRegistrarProtocolo")
print(confirmar.is_enabled()) # return True
print(confirmar.is_displayed()) # return True
print(confirmar.is_selected()) # return False
confirmar.submit() # doesn't work
confirmar.click() # doesn't work
click()
和submit()
什么都不做...
编辑
仅用鼠标,我也无法使用“ Tab”键到达“ Confirmar”按钮。
答案 0 :(得分:0)
我知道我来晚了一点,但是这个与交互性有关的特殊问题(即使您已经在DOM中找到一个元素的情况下)也可能会传播,因为我的经验有两个问题;其他JS元素实际上相对于它们在DOM中的位置是“在路上”,或者您正在处理输入等不规则的元素类型。
这两种情况下的解决方法通常是直接调用JS,而不是像这样通过execute_script()通过Python:
confirmar = driver.find_element_by_id("btnRegistrarProtocolo")
driver.execute_script('arguments[0].click();', confirmar)
如果您发现这不起作用,那是因为页面上有多个具有相同选择器的元素,在这种情况下,您应该通过XPath找到该元素。您可以像这样通过在find_elements()上检查长度来进行测试:
registrars = driver.find_elements(by='id','btnRegistrarProtocolo')
或
registrars = driver.find_elements_by_id('btnRegistrarProtocolo')
if len(registrars) > 1:
print('Wow I might've been clicking the wrong element this whole time!')
作为一种规范性标准,我总是利用XPath来定位元素,这仅仅是因为如果正确使用它,在与您交互的方式上几乎没有歧义。
答案 1 :(得分:-1)
文本为 Confirmar 的元素是启用了JavaScript的元素,因此您必须为element_to_be_clickable()
引入 WebDriverWait ,并且可以使用其中一个以下Locator Strategies中的
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#btnRegistrarProtocolo[value='Confirmar']"))).click()
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='btnRegistrarProtocolo' and @value='Confirmar']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC