C#-如何使用HTMLDocument.getElementById和InternetExplorer对象在框架内拾取元素

时间:2018-07-22 20:44:38

标签: c# mshtml shdocvw.internetexplorer

我有一个html,看起来像这样

<html>
    <head></head>
    <frameset id="workFrame" rows="45,*" border="0" frameSpacing="0">
        <frame name="menuFrame" id="menuFrame" src="Test.aspx" frameBorder="0" noResize="noresize" scrolling="no">
            <html>
                <head></head>
                <body>
                    <form id="form1" action="./Test.aspx" method="post">
                        <div class="firstDiv"></div>
                        <div class="firstDiv"></div>
                        <div class="secondDiv">
                            <div class="secondDivHeader">
                                <table>
                                    <tbody>
                                        <tr>
                                            <td></td>
                                            <td class ="secondTD">
                                                <table>
                                                    <tbody>
                                                        <tr>
                                                            <td>
                                                                <a id ="anchorOne"></a>
                                                                <a id ="anchorTwo" href="Click.html"></a>
                                                                <a id ="anchorThree"></a>
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </td>
                                            <td></td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>  
                </body>
            </html>
    </frameset>
</html
  

我想获取“ anchorTwo”并将其放入HTMLAnchorElement中,这样   我可以单击它。但是,当我尝试使用我的代码进行操作时,我得到了一个    NULL HTMLAnchorElement。有谁知道解决此问题的方法?我已经尝试了好几个小时,似乎找不到办法。

这是我的代码:

InternetExplorer ieObject = new InternetExplorer();
HTMLDocument htmlDocObject = null;
ieObject.Visible = true;
ieObject.Navigate("http://samplewebsite.com");

while (ieObject.ReadyState != tagREADYSTATE.READYSTATE_COMPLETE || ieObject.Busy)
{
    Console.WriteLine("Sleeping for 2 seconds...");
    Thread.Sleep(2000);
}

Console.WriteLine($"Website loaded");
htmlDocObject = ieObject.Document;

HTMLFrameElement frame = (HTMLFrameElement)htmlDocObject.getElementById("menuFrame");
Console.WriteLine(frame.name);
Console.WriteLine(frame.src);

HTMLDocument frameDocument = frame.document;

HTMLAnchorElement anchor = (HTMLAnchorElement)frameDocument.getElementById("anchorTwo"); \\null pointer here

anchor.click();

Console.ReadLine();

1 个答案:

答案 0 :(得分:0)

使用

    var d = new List<double>();
    for (var dd = -0.05; dd < 0.05; dd += 0.05)
    {
        d.Add(dd);
    }

代替

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

url ="http://sugang.korea.ac.kr"
driver = webdriver.Chrome()
driver.get(url)
WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.CSS_SELECTOR,'[name=firstF]')))
driver.switch_to.frame(driver.find_element_by_css_selector('[name=firstF]'))
WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.ID,'id'))).send_keys('abc')
driver.find_element_by_id('pw').send_keys('def')
driver.find_element_by_id('loginButton').click()