Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python使用selenium webdriver通过xpath查找元素_Python_Selenium - Fatal编程技术网

使用python使用selenium webdriver通过xpath查找元素

使用python使用selenium webdriver通过xpath查找元素,python,selenium,Python,Selenium,我想用python将文件上传到onedrive上。但我找不到上传按钮。在驱动程序中找到xpath元素,但在编写代码时引发超时异常 HTML代码: <div class="CommandBarItem beak-anchor command is-focused" data-bind="css: { 'is-toggled': isOpen() || isToggled(), 'is-disabled': isDisabled, 'is-disabled-invisible': isInvi

我想用python将文件上传到onedrive上。但我找不到上传按钮。在驱动程序中找到xpath元素,但在编写代码时引发超时异常

HTML代码:

<div class="CommandBarItem beak-anchor command is-focused" data-bind="css: { 'is-toggled': isOpen() || isToggled(), 'is-disabled': isDisabled, 'is-disabled-invisible': isInvisibleWhenDisabled, 'is-open': isOpen, 'is-focused': focused, 'CommandBarItem--hasPayload': $component.hasPayload, 'icon-only': hasIconOnly, 'is-current-view': isCurrentView, 'is-pivot': isCurrentView !== null, 'is-last-pivot': isLastPivot }, class: commandClass() ? 'od-Command--' + commandClass() : '', hasFocus: isActive, raiseEventOnChange: { eventName: 'layoutChange', bubbleEvent: true, data: layoutChanged }, dismiss: { isOpen: isOpen, isAutomatic: payload &amp;&amp; payload.autoDismiss, timeout: payload &amp;&amp; payload.timeout, dismissOnResize: false, dismissOnScroll: false }, with: targetCommand(), attr: { tabindex: tabindex, role: role, 'aria-label': $parent.accessibleLabel, 'aria-haspopup': visibleChildren().length > 0 || !!payload, 'aria-selected': isOpen(), 'aria-pressed': isToggled(), 'data-automationid': automationId, 'aria-expanded': visibleChildren().length > 0 ? isOpen().toString() : false, id: newFeatureExperienceAnchor }, teachingBubble: teachingBubble, tooltip: { content: $data.tooltip, isTooltipDisabled: isOpen }" tabindex="0" role="menuitem" aria-label="Upload. Upload files from your computer to this location" aria-haspopup="true" data-automationid="" aria-expanded="false">
   <div class="CommandBarItem-linkWrapper">
      <div class="CommandBarItem-link" data-bind="measure: { measure: $parent.measurementReady, async: true }, click: onClick, automation: 'link'" data-automationid="link">
         <!--ko if: $parent.icon--><span class="CommandBarItem-icon" data-bind="component: { name: 'od-icon-glyph', params: { icon: $parent.icon, badge: $parent.iconBadge } }, attr: { 'is-flipped': $parent.positionIconRight }"><i class="od-IconGlyph ms-Icon ms-Icon--Upload css-46 od-IconGlyph--visible" data-bind="class: iconClasses, attr: { 'data-icon-name': icon }, css: { 'od-IconGlyph--badged': badge, 'od-IconGlyph--visible': icon }, with: badge" data-icon-name="Upload"></i></span><!--/ko--><!--ko if: $parent.iconUrl--><!--/ko--><!--ko if: $parent.label()--><span class="CommandBarItem-commandText" data-bind="text: $parent.label, attr: { 'is-flipped': $parent.positionIconRight, 'role': $parent.hasDynamicText ? 'status' : null, 'aria-live': $parent.hasDynamicText ? 'polite' : null }, css: { 'CommandBarItem-commandText--isStrong': $parent.isStrong, 'ms-accessible': $parent.hasIconOnly() || !$parent.isLabelVisible() }">Upload</span><!--/ko--><!--ko if: visibleChildren().length && !hidesMenuChevron && !isCollapsed() && $parent.isLabelVisible()--><i class="od-CommandBarItem-down ms-Icon css-47" data-bind="class: getChevronDown()"></i><!--/ko-->
      </div>
      <!--ko if: $data.inputType() === ActionInputType.htmlFileUpload--><!--/ko--><!--ko if: $data.inputType() === ActionInputType.folderUpload--><!--/ko-->
   </div>
   <!--ko if: children().length && isOpen() && !isClipped()--><!--/ko--><!--ko if: $component.hasPayload--><!--/ko-->
</div>
driver.implicitly_wait(30)
try:
 df = wait.until ( EC.presence_of_element_located ( (By.XPATH, '//*[@id="appRoot"]//div[@class="CommandBarItem beak-anchor command is-focused" and @aria-label="Upload. Upload files from your computer to this location"]')))
 df.click ()
 print (df)
except TimeoutException:
    print ( "page time to load" )

但是它不起作用并抛出超时异常

有一种解决方法,您可以获取元素的位置,然后使用该位置单击该元素。或者您可以尝试使用javascript executer调用onclick方法。

如果您尝试自己调试,会发生什么?每个浏览器都应该有某种XPATH扩展,以查看您的查询是否匹配。当您有一个
等待时,为什么您是
驱动程序。隐式等待(30)
?查看上载链接的
标记。如果我删除驱动程序,它也会隐式抛出超时错误