首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新值不使用selenium python从表中检索。

更新值不使用selenium python从表中检索。
EN

Stack Overflow用户
提问于 2018-08-15 10:49:48
回答 2查看 51关注 0票数 0

我试图通过在Python中使用Selenium从下面的代码中访问>.<之间的数据。

代码语言:javascript
复制
<tbody>
                        <tr>                
                                <td>
    <div class="answer-votes" title="Asked 8 non-wiki questions with a total score of 164. Gave 84 non-wiki answers with a total score of 337." onclick="window.location.href='/search?q=user:37181+[python]'">337</div>
    <a href="/search?q=user:37181+[python]" class="post-tag" title="">python</a>
    <span class="item-multiplier" title="93 posts in the python tag"><span class="item-multiplier-x">×</span>&nbsp;<span class="item-multiplier-count">93</span></span></td>
                                <td>
    <div class="answer-votes" title=" Gave 4 non-wiki answers with a total score of 22." onclick="window.location.href='/search?q=user:37181+[django-templates]'">22</div>
    <a href="/search?q=user:37181+[django-templates]" class="post-tag" title="">django-templates</a>
    <span class="item-multiplier" title="4 posts in the django-templates tag"><span class="item-multiplier-x">×</span>&nbsp;<span class="item-multiplier-count">4</span></span></td>
                                <td>
    <div class="answer-votes" title=" Gave 1 non-wiki answer with a total score of 12." onclick="window.location.href='/search?q=user:37181+[slug]'">12</div>
    <a href="/search?q=user:37181+[slug]" class="post-tag" title="">slug</a>
    </td>
                                <td>
    <div class="answer-votes" title=" Gave 1 non-wiki answer with a total score of 8." onclick="window.location.href='/search?q=user:37181+[google-app-engine]'">8</div>
    <a href="/search?q=user:37181+[google-app-engine]" class="post-tag" title=""><img src="//i.stack.imgur.com/vobok.png" height="16" width="18" alt="" class="sponsor-tag-img">google-app-engine</a>
    </td>
                        </tr>
                        <tr>                
                                <td>
    <div class="answer-votes" title="Asked 1 non-wiki question with a total score of 89. Gave 56 non-wiki answers with a total score of 235." onclick="window.location.href='/search?q=user:37181+[django]'">235</div>
    <a href="/search?q=user:37181+[django]" class="post-tag" title="">django</a>
    <span class="item-multiplier" title="57 posts in the django tag"><span class="item-multiplier-x">×</span>&nbsp;<span class="item-multiplier-count">57</span></span></td>
                                <td>
    <div class="answer-votes" title="Asked 1 non-wiki question with a total score of 21. Gave 1 non-wiki answer with a total score of 22." onclick="window.location.href='/search?q=user:37181+[clang]'">22</div>
    <a href="/search?q=user:37181+[clang]" class="post-tag" title="">clang</a>
    <span class="item-multiplier" title="2 posts in the clang tag"><span class="item-multiplier-x">×</span>&nbsp;<span class="item-multiplier-count">2</span></span></td>
                                <td>
    <div class="answer-votes" title=" Gave 1 non-wiki answer with a total score of 12." onclick="window.location.href='/search?q=user:37181+[connect]'">12</div>
    <a href="/search?q=user:37181+[connect]" class="post-tag" title="show all posts by this user in 'connect'">connect</a>
    </td>
                                <td>
    <div class="answer-votes" title=" Gave 1 non-wiki answer with a total score of 8." onclick="window.location.href='/search?q=user:37181+[memcached]'">8</div>
    <a href="/search?q=user:37181+[memcached]" class="post-tag" title="">memcached</a>
    </td>
                        </tr>
                </tbody>

但是,当编译器移到下一个<td>时,我的程序没有显示更新后的<td>值。你能指导我如何解决这个问题吗?这是我的代码:

代码语言:javascript
复制
driver.get("https://stackoverflow.com/users/37181/alex-gaynor?tab=tags")
SMRTable = driver.find_elements_by_xpath("//*[@class='user-tags'] //td")



for i in SMRTable:

    print(i.get_attribute('innerHTML'))
    print(i.find_element_by_xpath("//div[@class='answer-votes']").get_attribute('innerHTML'))
    print(i.find_element_by_xpath("//*[@class='post-tag']").get_attribute('innerHTML'))
    print(i.find_element_by_xpath("//span[@class='item-multiplier-count']").get_attribute('innerHTML'))
    print('\n')
EN

回答 2

Stack Overflow用户

发布于 2018-08-15 10:54:53

如果要处理table中的每个table,则需要在每个XPath表达式的开头指定点(上下文字符),例如替换

代码语言:javascript
复制
print(i.find_element_by_xpath("//div[@class='answer-votes']").get_attribute('innerHTML'))

使用

代码语言:javascript
复制
print(i.find_element_by_xpath(".//div[@class='answer-votes']").get_attribute('innerHTML'))

否则,您将在每次迭代中获得相同的值(仅来自第一个td的值)。

还请注意,不应使用get_attribute('innerHTML')获取节点的文本内容,而应使用text属性:

代码语言:javascript
复制
print(i.find_element_by_xpath(".//div[@class='answer-votes']").text)
票数 2
EN

Stack Overflow用户

发布于 2018-08-15 18:52:54

你的代码尝试近乎完美。你需要处理一些额外的事情:

  • find_elements_by_xpath() for SMRTable时,添加tagName,即table
  • 在打印innerHTML时,当您引用SMRTable中的元素时,您必须通过innerHTML(点运算符)设置引用。
  • //div[@class='answer-votes']是直接子标记,因此将其更改为./div[@class='answer-votes']
  • //*[@class='post-tag']总是在<a>标记中,所以您需要使用.//a[@class='post-tag']
  • 您的有效代码将是: driver.find_elements_by_xpath("//table@class='user-tags'//tr/td") (“https://stackoverflow.com/users/37181/alex-gaynor?tab=tags") SMRTable =SMRTable中的I: print(i.find_element_by_xpath("./div@class='answer-votes'").get_attribute('innerHTML')) print(i.find_element_by_xpath(".//a@class='post-tag'").get_attribute('innerHTML'))print(i.find_element_by_xpath(".//span@class='item-multiplier-count'").get_attribute('innerHTML'))
  • 控制台输出: 337 python 93 22 django-模板4
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51857320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档