簡體   English   中英

使用 python selenium 定位元素文本

[英]Locate element text with python selenium

我嘗試從具有 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)

I know how to locate it with xpath and css selector, but it's moving (elements change position sometimes) and that's why the xpath is changing.

您可以使用 class 屬性來定位元素:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM