简体   繁体   English

python:硒需要使用Xpath的参数文本

[英]python : selenium need parameter text using Xpath

The HTML code is HTML代码是

<div class="_4w5j">
    <strong>
        <span>username</span>
    </strong>
    <span class="_-kj">—</span>
    <span class="_4w5k">
        <i class="img sp_4YfKd3lpYjo sx_5f57b1">
            <u>5 star</u>
        </i>
    </span>
    <span>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</span>
</div>
  1. I need the text "username" inside strong/spam It is works for me the code is 我需要在强/垃圾邮件中输入文本“用户名”,这对我有用,代码为

x= i.find_elements_by_xpath('//div[@class="_4w5j"]/strong') x = i.find_elements_by_xpath('// div [@class =“ _ 4w5j”] / strong')

for d in x:
    print d.text

It's print the correct result 打印正确的结果

I need the text of 'x'inside the span[3] 'xxxxxxxxxxxxxxx' the below code also works good. 我需要span [3]'xxxxxxxxxxxxxxx'内的'x'文本,以下代码也可以正常工作。

Question: 题:

I need the text inside the u tag '5 star'. 我需要u标签“ 5星”内的文本。 I used the below code but it prints the empty result. 我使用了下面的代码,但它显示了空结果。

y= i.find_elements_by_xpath('//div[@class="_4w5j"]/descendant::u')
for d in y:
    print d.text

I tried this also 我也尝试过

y= i.find_elements_by_xpath('//div[@class="_4w5j"]/span[2]/i/u')
for d in y:
    print d.text

Not working for me. 不为我工作。 Please help me . 请帮我 。 Thanks in advance 提前致谢

<a class="uiLinkSubtle" href="/sherryl.cheary/activity/10152833559161998">
<abbr class="timestamp" data-utime="1419713801" title="Saturday, December 27, 2014 at 12:56pm">about a month ago</abbr>
</a>

I need the text inside the title but the below code sh 我需要标题中的文本,但下面的代码sh

z= i.find_elements_by_xpath('//a/descendant::abbr/@title')
for dd in z:
    print dd.text

Its shows the error called " web element has no attr title or some thing wrong in xpath" 它显示了一个错误,即“ Web元素没有attr标题或xpath中有某些错误”

i.find_elements_by_xpath('//div[@class="_4w5j"]/span//u')[0].text

i.find_elements_by_xpath('//a/abbr')[0].get_attribute('title')

Why do you stick with xpath? 您为什么坚持使用xpath?

Consider this 考虑一下

 find_element_by_tag_name('u').text         

 find_element_by_tag_name('abbr').get_attribute('title')

You have to adjust it to search for elements if you have multiple u and abbr tags. 如果您有多个uabbr标签,则必须对其进行调整以搜索elements

Anyway this will be much more readable for you in future. 无论如何,将来您会更容易理解。

In may opinion you should not overuse xpath stuff, especially if you can get what you need with simpler methods like lookup by tag_name or class or css_selector 在5月的意见,你不应该过度使用xpath东西,特别是如果你可以得到你需要与像查找简单的方法是什么tag_nameclasscss_selector

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM