无法点击链接,如嵌套标签<div> <tag>&#34;内容&#34; </tag> </div>

时间:2016-03-04 11:47:06

标签: python-3.x selenium

html代码:           

<div id="content1" class="hide"><strong>Own Account</strong><br/>Transfer funds to your own accounts in the same branch or other branches.</div>
<div id="content2" class="hide"><strong>Accounts of Others</strong><br>Transfer funds to Intra-Bank beneficiary accounts in the same bank (same branch or other branches).</div>
<div id="content3" class="hide"><strong>Inter-Bank Beneficiary</strong><br>Transfer funds to beneficiary accounts in other banks including State Bank Group Associate Banks.</div>
<div id="content4" class="hide"><strong>Issue Demand Draft</strong><br>Send request for issue a draft online using funds in a transaction accounts, and select the branch at which the draft is payable.</div>

<div id="content8" class="hide"><strong>Donations</strong><br>Donate online to religious and charitable institutions.</div>


<!-- <div id="content7" class="hide"><strong>Credit Card (VISA) Bill Pay</strong><br>Transfer funds to any Credit Card (VISA) Bill using funds in your transaction accounts.</div> -->
<div id="content7" class="hide"><strong>Credit Card (VISA) Beneficiary</strong><br>Transfer funds to any Credit Card (VISA)using funds in your transaction accounts.</div>
<div id="content9" class="hide"><strong>NRI eZ Trade Funds Transfer</strong><br>If you are an NRI, you can transfer funds from parent account to the NRE PIS account to trade in stock. </div>

我想要获取&#34;拥有帐户&#34;从这个标签中点击它。

我试过了:

elem = driver.find_element_by_id("content2").click()

但这不会起作用,错误是

selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with

堆栈跟踪:

我猜它试图在<strong></strong>标记内访问自己的帐户,因此无法找到,

但如何解决这个问题会让我烦恼。

我的代码作为一个整体是:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("https://www.onlinesbi.com")
assert "State Bank of India" in driver.title
login_link = driver.find_element_by_link_text('LOGIN')
login_link.click()


login_link2 = driver.find_element_by_link_text('CONTINUE TO LOGIN')
login_link2.click()


usrname = driver.find_element_by_name("userName")
usrname.send_keys("*********")

passwrd = driver.find_element_by_name("password")
passwrd.send_keys("*******")


login_button = driver.find_element_by_xpath("//input[@title='Login'][@type='submit']")
login_button.click()


login_button2 = driver.find_element_by_link_text('Payments/Tra`enter code here`nsfers')
login_button2.click()

1 个答案:

答案 0 :(得分:0)

您获得的异常意味着当您尝试单击它时,您要单击的元素不可见。您可以使用显式等待来确保在单击

之前元素可见
wait = WebDriverWait(driver, 10)
elem = wait.until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "#content2 > strong")));
elem.click()

在点击它之前,这将等待最多10秒钟才能看到该元素。

另一种选择是使用JavaScript点击

driver.execute_script("document.getElementById('content2').click()")

或者

elem = driver.find_element_by_id("content2")
driver.execute_script("arguments[0].click();", elem)

或者

elem = driver.find_element_by_css_selector("#content2 > strong")
driver.execute_script("arguments[0].click();", elem)