selenium click()跳过

时间:2016-09-16 01:27:42

标签: python selenium-webdriver

我最近一直在做一些关于selenium的工作,我遇到了click()函数的问题。

给出以下HTML代码:

    <div id="sendreply">
      <input type="submit" class="button norm-green" value=
      "Send Message name="sendmessage"> == $0
    </div>

我一直试图点击“发送消息”按钮,但是click()只是通过动作,程序进入下游操作。

我尝试过使用name和xpath查找元素:

    time.sleep(2)
    option='by_name'
    if option == 'by_name':
        driver.find_element_by_name('sendmessage').click()
    else:
        driver.find_element_by_xpath("//div[@id='sendreply']").click()

我没有得到任何错误代码。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

如果您已经尝试了所有但没有取得成功,请尝试使用JavaScript作为替代解决方案来执行点击并摆脱此问题,如下所示: -

HTMLElement#click()

警告: - (focus, blur, mousedown, mouseup...)注射String warurl = "http://.../file.war"; try{ URL url = new URL(warurl); InputStream inputStream = url.openStream().getClass().getResourceAsStream("META-INF/MANIFEST.MF"); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); }catch(Exception e){ System.out.println("Error reading manifest file."); } 不应在测试环境中使用。它违背了测试的目的。首先是因为它不会像真正的点击<?php function getPagesIndexedGoogle($site) { if ($site) { $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => "https://www.google.com.au/search?q=site:$site&amp;gws_rd=ssl", CURLOPT_SSL_VERIFYPEER=> false, CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko' )); $result_string = curl_exec($curl); curl_close($curl); if (strpos($result_string, "did not match any documents") !== false) { return 0; } else { $match = preg_match("/about ([0-9,]{0,12})/i", $result_string, $matches); echo $matches[1]; } } } if(!empty($_POST['domain'])){ // Tries to split URLs by new line or space character $sites = preg_split('#\\r\\n|\\r|\\n| #i', $_POST['domain']); foreach($sites as $site){ // Checks if the URL is a valid website or not (http(s):// must be included!) if(preg_match('#((https?|ftp):\/\/(\S*?\.\S*?))([\s)\[\]{},;"\':<]|\.\s|$)#i', $site)){ echo $site; echo getPagesIndexedGoogle($site); } else { echo $site; echo " is not a valid url."; } } } else { echo "No websites were entered."; } ?> 那样生成所有事件,因为它并不能保证真实用户可以与元素进行交互。但是你可以将它作为一种替代解决方案来摆脱这个问题。