首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python | Selenium无法检测按钮

Python | Selenium无法检测按钮
EN

Stack Overflow用户
提问于 2018-05-21 06:47:43
回答 1查看 82关注 0票数 0
代码语言:javascript
复制
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
import os

Game_Pin = input('Enter your PIN: ')
NickNAME = input('Enter your nickname: ')
driver = webdriver.Chrome(executable_path=r"C:\WebDriver\bin\chromedriver.exe")

def Enter_Press(driver):
    driver.find_element_by_xpath("//*[contains(text(), 'Enter')]").click()

def OK_GO(driver):
    driver.find_element_by_xpath("//*[contains(text(), 'OK, go!')]").click()


def Kahoot_Spammer(Game_Pin, NickNAME, driver):
    driver.get('https://kahoot.it/')
    driver.maximize_window() #For maximizing window
    driver.implicitly_wait(2) #gives an implicit wait for 2 seconds

    game_pin = driver.find_element_by_xpath("//*[@id='inputSession']")
    game_pin.send_keys(Game_Pin)
    Enter_Press(driver)

    driver.implicitly_wait(2)
    Name = driver.find_element_by_xpath("//*[@id='username']")
    Name.send_keys(NickNAME)
    OK_GO(driver)




Kahoot_Spammer(Game_Pin, NickNAME, driver)

这就是代码。它应该打开chrome浏览器并导航到Kahoot.it网站。然后拿出你给它的信息,然后把它放进去。它适用于进入游戏的第一部分,但是一旦它创建了你的昵称,它就无法检测到OK,go!按钮。

代码语言:javascript
复制
driver.find_element_by_xpath("//*[contains(text(), 'OK, go!')]").click()

我已经检查了按钮,但似乎找不到要在上面的代码中放置什么。有什么想法吗?

这是源代码。

代码语言:javascript
复制
<!doctype html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]>    <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]>    <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if IE 9]>    <html class="no-js lt-ie10" lang="en"> <![endif]-->
<!--[if gt IE 9]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

  <title>Kahoot!</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, minimum-scale=1.0"/>
  <meta name="viewport" content="initial-scale=1, maximum-scale=1.0, minimum-scale=1.0" media="(device-height: 568px)"/>
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-itunes-app" content="app-id=1131203560">

  <meta name="description" content="Join a game of kahoot here. Kahoot! is a free game-based learning platform that makes it fun to learn – any subject, in any language, on any device, for all ages!">
  <meta name="keywords" content="education, platform, smart phone, tablet, mobile, social, inclusive, HTML5, classroom, engagement, play, game, fun, quiz, multi-player, pedagogy, learning model, learn, gamification." />


  <link rel="shortcut icon" href="/shared/theme/kahoot/img/icons/favicon.ico">
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/shared/theme/kahoot/img/icons/touch_icon_144.png">
  <link rel="apple-touch-icon-precomposed" sizes="114x114" href="/shared/theme/kahoot/img/icons/touch_icon_114.png">
  <link rel="apple-touch-icon-precomposed" sizes="72x72" href="/shared/theme/kahoot/img/icons/touch_icon_72.png">
  <link rel="apple-touch-icon-precomposed" href="/shared/theme/kahoot/img/icons/touch_icon_57.png">

  <link rel="stylesheet" type="text/css" href="/shared/css/cloak.css">

  <div style="height: 0; width: 0; position: absolute; visibility: hidden">
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><filter x="-2.2%" y="-2.3%" width="104.4%" height="104.8%" filterUnits="objectBoundingBox" id="a"><feOffset dy="1" in="SourceAlpha" result="shadowOffsetOuter1"/><symbol id="logo-shapes" viewBox="0 0 24 24"><ellipse cx="5.506" cy="18.966" rx="4.953" ry="4.953"/><path d="M12.005 5.902L17.873.033l5.869 5.869-5.869 5.868zm1.443 8.899h8.849v8.849h-8.849zm-2.584-4.977H.146l5.36-8.555z"/></symbol></svg>
  </div>

  <script src="https://tap-nexus.appspot.com/js/sdk/kahunaAPI_min.js"></script>

  <script type="text/javascript">
    (function(e,t){var n=e.amplitude||{_q:[],_iq:{}};var r=t.createElement("script");r.type="text/javascript";
    r.async=true;r.src="https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-3.4.0-min.gz.js";
    r.onload=function(){e.amplitude.runQueuedFunctions()};var i=t.getElementsByTagName("script")[0];
    i.parentNode.insertBefore(r,i);function s(e,t){e.prototype[t]=function(){this._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
    return this}}var o=function(){this._q=[];return this};var a=["add","append","clearAll","prepend","set","setOnce","unset"];
    for(var u=0;u<a.length;u++){s(o,a[u])}n.Identify=o;var c=function(){this._q=[];return this;
    };var p=["setProductId","setQuantity","setPrice","setRevenueType","setEventProperties"];
    for(var l=0;l<p.length;l++){s(c,p[l])}n.Revenue=c;var d=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenueV2","regenerateDeviceId","logEventWithTimestamp","logEventWithGroups"];
    function v(e){function t(t){e[t]=function(){e._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
    }}for(var n=0;n<d.length;n++){t(d[n])}}v(n);n.getInstance=function(e){e=(!e||e.length===0?"$default_instance":e).toLowerCase();
    if(!n._iq.hasOwnProperty(e)){n._iq[e]={_q:[]};v(n._iq[e])}return n._iq[e]};e.amplitude=n;
    })(window,document);
  </script>

  <base href="/">

  <script type="text/javascript">
    document.write('<scri'+'pt ');
    document.write('type="text/javascript" ');
    document.write('src="'+'/shared/theme/config.js');
    document.write("?"+new Date().getTime()+'">');
    document.write('</scri'+'pt>');
  </script>
</head>
<body snitch ios7-viewport-fix>
  <noscript>
    <h1>Kahoot! needs JavaScript to work</h1>

    <p>
      To use Kahoot!, you need to have JavaScript enabled in your browser. To enable JavaScript, please do the following:
    </p>
     <ul>
      <li><a href="http://enable-javascript.com/">Follow these instructions</a>.</li>
      <li>Make sure you have the <a href="https://www.mozilla.org/en-US/firefox/new/">latest browser</a>.</li>
      <li>Turn off or disable the NoScript extension, if you have it.</li>
      <li>Contact your IT administrator to allow access to Kahoot! in your security preferences.</li>
    </ul>

    <p>If you continue to have problems, please let us know by <a href="http://kahoot.uservoice.com/">contacting Kahoot! support</a>.</p>
  </noscript>
  <div id="debug-info" debug-info="dev,test" debug-timestamp></div>
  <dev-mode></dev-mode>

  <div class="loader" loader></div>

  <iframe
    id="gameBlockIframe"
    style="display:none;"
    class="game-block-iframe"
    sandbox="allow-scripts allow-same-origin"
    scrolling="no">
  </iframe>

  <div id="mainView" ng-cloak ng-view>
    <h1>Join in a Kahoot! here</h1>
    <p>To learn more about Kahoot! visit <a href="https://kahoot.com/?utm_source=controller&utm_campaign=controller_app" target="_system">kahoot.com</a></p>
  </div>

  <div ng-cloak alerts></div>

  <script type="text/javascript" src="/js/bootstrap.js"></script>
  <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

function gup( name, url ) {
    if (!url) url = location.href;
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( url );
    return results == null ? null : results[1];
}



var clientId = gup('gaId', window.location.search);

if (clientId) {
  ga('create', 'UA-35308575-1', 'auto', {'allowLinker': true, 'clientId':gup('gaId', window.location.search)});
  ga('create', 'UA-35308575-4', 'auto', {'name': 'legacy', 'clientId':gup('gaId', window.location.search)});
  var platform = gup('platform', window.location.search);

  if (typeof platform === 'string' && platform == 'iOS') {
          window.ga('set', 'appName', 'Kahoot');
          window.ga('set', 'appId', 'no.mobitroll.kahoot.controller');
        }
  if (typeof platform === 'string' && platform == 'Android') {
          window.ga('set', 'appName', 'Kahoot');
          window.ga('set', 'appId', 'no.mobitroll.kahoot.android');
        }
} else {
  ga('create', 'UA-35308575-1', 'auto', {'allowLinker': true});
  ga('create', 'UA-35308575-4', 'auto', {'name': 'legacy'});
}


  ga('send', 'pageview');
  ga('legacy.send', 'pageview');

  </script>

</body>
</html>
EN

回答 1

Stack Overflow用户

发布于 2018-05-21 19:44:54

在初始化implicitly_wait时,只需将声明为一次。要显式等待指定的秒数,可以使用time.sleep(),尽管更实用的解决方案是使用Selenium的WebDriverWait动态等待元素出现/可单击。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50440066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档