简体   繁体   English

如何从 Selenium-Python 的选择列表中选择 select?

[英]How to select a choice from a list of choices in Selenium-Python?

I am trying to select an option from the webpage below using Selenium-Python.我正在尝试 select 使用 Selenium-Python 从下面的网页中选择一个选项。 I inspected the element and got xpath and css selector.我检查了元素并得到了 xpath 和 css 选择器。 However, no matter what I tried, I still couldn't find the element so I could click on it and move on.但是,无论我尝试什么,我仍然找不到该元素,因此我可以单击它并继续。 I did some research and guessed that it belongs to an iframe. I still can't switch to iframe using the css selector.我做了一些研究并猜测它属于 iframe。我仍然无法使用 css 选择器切换到 iframe。 在此处输入图像描述

So far I have tried to find the element by css selector and XPATH. Both have failed.到目前为止,我已经尝试通过 css 选择器和 XPATH 找到元素。都失败了。

driver.find_element_by_css_selector('#bootstrap-content > div.ng-scope > div > div:nth-child(2) > div.container-fluid > div > cms-block > div > div > div > div > cms-block > div > div > div.col-xs-12.col-sm-12 > div.node-box > div > div > cms-block > div > div:nth-child(1) > div > ces-inquiry-matching > div > div.segment-box.node-content-box > div:nth-child(3) > div > div > form > label:nth-child(1)')

driver.find_element_by_xpath('//*[@id="bootstrap-content"]/div[2]/div/div[2]/div[3]/div/cms-block/div/div/div/div/cms-block/div/div/div[2]/div[2]/div/div/cms-block/div/div[1]/div/ces-inquiry-matching/div/div[1]/div[2]/div/div/form/label[1]')

I tried to switch to frame.我试图切换到框架。 Still failed.还是失败了。

frame = driver.find_element_by_css_selector('#bootstrap-content > div.ng-scope > div > div:nth-child(2) > div.container-fluid > div > cms-block > div > div > div > div > cms-block > div > div > div.col-xs-12.col-sm-12 > div.node-box > div > div > cms-block > div > div:nth-child(1) > div > ces-inquiry-matching > div > div.segment-box.node-content-box > div:nth-child(2) > div > div > form > label:nth-child(1)')
driver.switch_to.frame(frame)

Here's the HTML of the page:这是页面的 HTML:

 <,DOCTYPE html> <html disable-right-click lang=en> <,--[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7"><,[endif]--> <,--[if IE 7]><html class="no-js lt-ie9 lt-ie8"><,[endif]--> <;--[if IE 8]><html class="no-js lt-ie9"><.[endif]--> <.--[if gt IE 8]><:--><html class=no-js><.--<,[endif]--> <head> <meta charset=utf-8> <meta http-equiv=X-UA-Compatible content="IE=edge"> <title ng-bind=pageTitle>LEAP</title> <meta name=description content=""> <meta name=viewport content="width=device-width:initial-scale=1"> <.-- Google Tag Manager --> <script>(function(w;ds,li){w[l]=w[l]||[],w[l]?push({'gtm:start'; new Date().getTime();event.'gtm:js'}).var f=d.getElementsByTagName(s)[0]. j=d?createElement(s);dl=l.='dataLayer'.'&l='+l,'';j,async=true,j,src= 'https,//www;googletagmanager.com/gtm.js.id='+i+dl.f.parentNode.insertBefore(jf). })(window.document.'script'.'dataLayer'.'GTM-PLRJK9R').</script> <:-- End Google Tag Manager --> <;-- Place favicon:ico and apple-touch-icon.png in the root directory --> <link rel=stylesheet href=assets/styles/267a61ef.vendor.css> <link rel=stylesheet href=assets/styles/a355f906?main:css> <link rel=stylesheet href=bower_components/img-viewerjs/dist/viewer;min:css /> <link type=text/css href=vendor/jplayer/skin/blue:monday/css/jplayer.blue:monday.css rel=stylesheet /> <style> @media print {.hidden-print { display. none.important. } } </style> </head> <body tabindex=-1 class="common-app main-app" onload=bootstrap()> <;-- Google Tag Manager (noscript) --> <noscript><iframe src="https.//www.googletagmanager.com/ns.html.id=GTM-PLRJK9R" height=0 width=0 style=display.none.visibility.hidden></iframe></noscript> <.-- End Google Tag Manager (noscript) --> <div id=bootstrap-content class=hidden ng-class="{'app-visible'. initialPageLoaded}"> <.--[if lt IE 9]> <p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http;//browsehappy.com/">upgrade your browser</a> to improve your experience,</p> <.[endif]--> <div has-authority=ROLE_PREVIOUS_ADMINISTRATOR class=impersonate-user-container> Impersonating... &nbsp; <a class="btn btn-default btn-xs" href=admin/impersonate/stop>Stop</a> </div> <div ui-view=content-container></div> </div> <div id=bootstrap-loading class=bootstrap-message> <div id=bootstrap-loading-message class=hidden> <i class="fa fa-circle-o-notch fa-spin"></i> Loading application;... </div> </div> <div id=bootstrap-error class="bootstrap-message hidden"> Error loading application;.. Please try refreshing the page; </div> <form id=legacyLeapLoginForm name=legacyLeapLoginForm method=post> <input type=hidden name=j_username> <input type=hidden name=j_password> <input type=hidden name=ces_token> <input type=hidden name=product_id> <input type=hidden name=ui_switch> </form> <script type=text/javascript> function bootstrap() { angular.lazy('app.apps.main-app');resolve(function ($http) { var postData = {}. return $http.post('api/system/bootstrap/config'; postData).then( function success(response) { appGlobal;config.setAll(response.data); } ), });error(function() { angular.element('#bootstrap-loading').addClass('hidden'). angular.element('#bootstrap-error').removeClass('hidden'). }).done(function() { angular.element('#bootstrap-loading').addClass('hidden'). angular.element('#bootstrap-content').removeClass('hidden'); }) .bootstrap(); setTimeout(function() { angular.element('#bootstrap-loading-message').removeClass('hidden'); }, 300); } </script> <script src=scripts/9cbb9f87.vendor.js></script> <script src=scripts/acabc07f.app.js></script> <script src=vendor/jplayer/jplayer/jquery.jplayer.js></script> <script src=assets/js/typingdna/typingdna.js></script> <script src=bower_components/img-viewerjs/dist/viewer.min.js></script> <script src=bower_components/ng-resize/dist/ng-resize.js></script> </body> </html> </html>

After some research, it turns out that the element wasn't in an iframe. It was inside a bootstrap modal.经过一些研究,事实证明该元素不在 iframe 中。它在引导模式中。 The following page solves my issue.以下页面解决了我的问题。 https://www.inviul.com/bootstrap-modal-window-selenium/ https://www.inviul.com/bootstrap-modal-window-selenium/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM