【问题标题】:Locate element text with python selenium使用 python selenium 定位元素文本
【发布时间】:2020-12-10 20:28:34
【问题描述】:

我尝试使用 python selenium 从 java 元素中查找一些文本。

<div id="allBetsTable" data-gameid="89519742" style="transition-timing-function: cubic-bezier(0.1, 0.57, 0.1, 1); transition-duration: 0ms; transform: translate(0px, 0px) translateZ(0px);"><div class="bet_group_col cols1"><div><div class="bet_group"><div class="bet-title bet-title_justify"><span class="bet-title__star"></span> 
            Match-Ups, 1X2. Duel between Players (Goals)
         <!----></div> <div class="bets betCols3"><div class=""><span data-type="13087" class="bet_type">Match-Up Leigh Griffiths - Jonathan David - Celtic
        <!----> <!----> <!----></span> <span data-coef="3.755" class="koeff"><i>3.755</i></span></div><div class=""><span data-type="13089" class="bet_type">Match-Up Leigh Griffiths - Jonathan David - X
        <!----> <!----> <!----></span> <span data-coef="1.567" class="koeff"><i>1.567</i></span></div><div class=""><span data-type="13088" class="bet_type">Match-Up Leigh Griffiths - Jonathan David - Lille OSC
        <!----> <!----> <!----></span> <span data-coef="3.976" class="koeff"><i>3.976</i></span></div></div></div></div><div><div class="bet_group"><div class="bet-title bet-title_justify"><span class="bet-title__star"></span> 
            Match-Ups, Double Chance. Duel between Players (Goals)
         <!----></div> <div class="bets betCols3"><div class=""><span data-type="13090" class="bet_type">Match-Up Leigh Griffiths - Jonathan David , Double Chance - CelticX
        <!----> <!----> <!----></span> <span data-coef="1.106" class="koeff"><i>1.106</i></span></div><div class=""><span data-type="13092" class="bet_type">Match-Up Leigh Griffiths - Jonathan David , Double Chance - Celtic Lille OSC
        <!----> <!----> <!----></span> <span data-coef="1.931" class="koeff"><i>1.931</i></span></div><div class=""><span data-type="13091" class="bet_type">Match-Up Leigh Griffiths - Jonathan David , Double Chance - Lille OSCX
        <!----> <!----> <!----></span> <span data-coef="1.124" class="koeff"><i>1.124</i></span></div></div></div></div><div><div class="bet_group"><div class="bet-title bet-title_justify"><span class="bet-title__star"></span> 
            Match-Ups, Total. Duel between Players (Goals)
         <!----></div> <div class="bets betCols2"><div class=""><span data-type="13093" class="bet_type">Match-Up Leigh Griffiths - Jonathan David, Total Goals Over (0.5)
        <!----> <!----> <!----></span> <span data-coef="1.686" class="koeff"><i>1.686</i></span></div><div class=""><span data-type="13094" class="bet_type">Match-Up Leigh Griffiths - Jonathan David, Total Goals Under (0.5)
        <!----> <!----> <!----></span> <span data-coef="1.776" class="koeff"><i>1.776</i></span></div><div class=""><span data-type="13093" class="bet_type">Match-Up Leigh Griffiths - Jonathan David, Total Goals Over (1)
        <!----> <!----> <!----></span> <span data-coef="3.456" class="koeff"><i>3.456</i></span></div><div class=""><span data-type="13094" class="bet_type">Match-Up Leigh Griffiths - Jonathan David, Total Goals Under (1)
        <!----> <!----> <!----></span> <span data-coef="1.154" class="koeff"><i>1.154</i></span></div><div class=""><span data-type="13093" class="bet_type">Match-Up Leigh Griffiths - Jonathan David, Total Goals Over (1.5)
        <!----> <!----> <!----></span> <span data-coef="5.32" class="koeff"><i>5.32</i></span></div><div class=""><span data-type="13094" class="bet_type">Match-Up Leigh Griffiths - Jonathan David, Total Goals Under (1.5)
        <!----> <!----> <!----></span> <span data-coef="1.033" class="koeff"><i>1.033</i></span></div></div></div></div><div><div class="bet_group"><div class="bet-title bet-title_justify"><span class="bet-title__star"></span> 
            Match-Ups, Handicap. Duel between Players (Goals)
         <!----></div> <div class="bets betCols2"><div class=""><span data-type="13095" class="bet_type">Match-Up Leigh Griffiths - Jonathan David, Handicap Celtic (0)
        <!----> <!----> <!----></span> <span data-coef="1.682" class="koeff"><i>1.682</i></span></div><div class=""><span data-type="13096" class="bet_type">Match-Up Leigh Griffiths - Jonathan David, Handicap Lille OSC (0)
        <!----> <!----> <!----></span> <span data-coef="1.781" class="koeff"><i>1.781</i></span></div></div></div></div></div> </div>

我要定位这条线:

<span data-coef="1.686" class="koeff"><i>1.686</i></span></div><div class=""><span data-type="13094" class="bet_type">Match-Up Leigh Griffiths - Jonathan David, Total Goals Under (0.5)

我知道如何使用 xpath 和 css 选择器定位它,但它在移动(元素有时会改变位置),这就是 xpath 改变的原因。

【问题讨论】:

  • 你试过什么xpath/css?请提供一些代码。
  • allbets = driver.find_element_by_id("allBetsTable")
  • odds = allbets.find_element_by_css_selector("div &gt; div:nth-child(3) &gt; div &gt; div.bets.betCols2 &gt; div:nth-child(2) &gt; span.koeff").text
  • 但有时是div:nth-child(3) 而不是div:nth-child(2)
  • 所以我要查找的行中唯一的固定值是Total Goals Under (0.5)

标签: java python selenium xpath web-scraping


【解决方案1】:

您可以使用类属性来定位元素:

driver.find_element_by_class_name('koeff')
driver.find_element_by_class_name('bet_type')

【讨论】:

  • 也通过 xpath: //span[@data-coef='1.686']
  • 1.686 不固定
  • @Phil 根据什么文本/属性值,您究竟想获得什么数据?
猜你喜欢
  • 1970-01-01
  • 2020-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 2017-06-19
相关资源
最近更新 更多