【问题标题】:Selenium Select Child with Several Children all Named the Same ThingSelenium 选择有几个孩子都命名相同的孩子
【发布时间】:2021-08-23 13:04:12
【问题描述】:

一般来说,Selenium 和 Web 的新手,我在尝试使用 XPath 查找和元素时遇到了一个未找到元素的问题。

selenium.common.exceptions.NoSuchElementException

我一直在试图弄清楚它,但我似乎无法解开它。如果我错过了明显的事情,请提前道歉。但如果是简单的google一下,我还没有遇到过这个词。

当我查看检查元素时,我得到以下两个 XPath 选项

//*[@id='fundamental-analysis-summary-container']/ul/li[1]/div[2]/div[1]/div/div[1]/div
/html/body/div[3]/div[2]/div[1]/div[10]/div/div/div[2]/div/div/ul/li[1]/div[2]/div[1]/div/div[1]/div

我要抓取四个不同的孩子,它们都是这些值的变体,包括 li[1]、li[2]、li[3] 和 li[4]

我要特别提取的 html 行是来自以下内容的 componentValue。

<div class="rating-text" ng-bind="data.componentValue">84</div>

周围的html看起来像

<div id="foo-analysis-summary-container" class="foo-analysis-summary" data="fooAnalysis">
    <!----><ul class="analysis-items" ng-if="hasData == true">
        <li class="foo-analysis-item summary" title="Valuation" scs-url="fooData.scsUrl" category="Valuation" low-label="Overvalued" high-label="Undervalued" data="fooData.valuation">
    <!----><div class="fas-title" ng-if="mode == 'summary'">
        <div class="tooltip-overlay-container" label="Valuation" container-id="foo-analysis-summary-container" content="How much item is worth">
  <!---->
  <!----><a class="tooltip-label" ng-if="title == undefined &amp;&amp; label != undefined" title="Valuation" ng-click="onClick()" ng-bind="label">Valuation</a><!---->

  <div class="overlay-container" ng-class="{'isVisible': isVisible}" ng-style="{'top': overlayTop, 'left': overlayLeft, 'width': width }" style="width: 270px;">
    <div class="tooltip-pointer down-pointer ng-hide" ng-show="isUp == false" ng-style="{'left': arrowLeft}" aria-hidden="true"><div class="rsh_snapshot_sprite_img rsh_snapshot_sprite_pointer_up"></div></div>
    <div class="tooltip-overlay-content">
      <div class="rsh_snapshot_sprite_img rsh_snapshot_sprite_close_gray close-icon" ng-click="onClose()" role="button" tabindex="0"></div>
        <!----><div ng-if="label != undefined" class="tooltip-title" ng-bind="label">Valuation</div><!---->
        <!---->
        <!----><div ng-if="!hasTransclusion &amp;&amp; !contentHtml" class="tooltip-text">N/A</div><!---->

        <!---->
    </div>
    <div class="tooltip-pointer up-pointer ng-hide" ng-show="isUp == true" ng-style="{'left': arrowLeft}" aria-hidden="true"><div class="rsh_snapshot_sprite_img rsh_snapshot_sprite_pointer_up"></div></div>
  </div>
</div>
    </div><!---->
    <!---->
    <!----><div class="scale" ng-if="hasData == true">
        <div class="rating-cursor">
            <div class="rating-label" ng-style="{'left': 'calc(' + data.componentValue + '% - 10px)'}" style="left: calc(84% - 10px);">
                <div aria-atomic="true"><span class="screen-reader-only">current = </span><div class="rating-text" ng-bind="data.componentValue">84</div></div>
                <div class="triangle-cursor"></div>
            </div>
        </div>
        <div class="ruler">
            <div class="separator s1"></div>
            <div class="separator s2"></div>
            <div class="separator s3"></div>
            <div class="separator s4"></div>
            <div class="current-value" ng-style="{'width': data.componentValue + '%'}" style="width: 84%;"></div>
        </div>
        <div class="fas-labels">
            <div aria-atomic="true"><div class="start-value-label" ng-bind="lowLabel">Overvalued</div><span class="screen-reader-only"> = 0</span></div>
            <div aria-atomic="true"><div class="end-value-label" ng-bind="highLabel">Undervalued</div><span class="screen-reader-only"> = 100</span></div>
        </div>
    </div><!---->

    <!-- Detail Metrics -->
    <!---->

    <!---->
</li>
    ... repeated three times with different values ...
</div>

我尝试执行的代码如下。

browser.find_element_by_xpath("//*[@id='fundamental-analysis-summary-container']/ul/li[1]/div[2]/div[1]/div/div[1]/div")

我已经让它与 find_element_by_css_selector 一起工作,但从我对谷歌搜索的有限掌握的情况来看,由于 li 在这种情况下它不起作用,我必须使用 xpath。如果有帮助,css 选择器是

li.foo-analysis-item:nth-child(1) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2)

或者这个

#foo-analysis-summary-container > ul > li:nth-child(1) > div.scale > div.rating-cursor > div > div:nth-child(1) > div

【问题讨论】:

  • 错误是什么,您想对 web 元素做什么? selenium 没有找到 xpath 还是 selenium 没有与 web 元素交互??
  • 它给出的消息是selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element:@AaronCloud

标签: python html css selenium selenium-webdriver


【解决方案1】:

要让您的元素文本考虑到li,您可以使用以下 css 选择器:

el_text = driver.find_element_by_css_selector(".foo-analysis-item.summary:nth-of-type(1) .rating-text").text

或以下 xpath 之一:

el_text = driver.find_element_by_xpath("//li[1]//div[@class='rating-text']").text

el_text = driver.find_element_by_xpath("//li[1]//div[@ng-bind='data.componentValue']").text

【讨论】:

  • 我尝试了所有这些,起初它没有工作。但后来我有了一个好主意,向下滚动直到文本在屏幕上可见,你知道它有什么用。谢谢。
  • @Falderol 这可能是因为您不允许页面加载。尝试在代码中添加一些 browser.implictywaits 或 time.sleep 以减慢它的速度。如果 selenium 没有立即查看,它通常会抛出这种错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-13
  • 2022-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多