如何使用Selenium和Python从元素中打印部分文本 [英] How to print the partial text from an element using Selenium and Python

查看:117
本文介绍了如何使用Selenium和Python从元素中打印部分文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从

I am trying to get the text "Album Vol.1 [Every letter I sent you] LP (Normal Edition)" from http://www.ktown4u.com/iteminfo?grp_no=231307&goods_no=44363#dt_wrap01 under the description tab but I keep getting this error message.

selenium.common.exceptions.NoSuchElementException:
Message: no such element: Unable to locate element: 
{"method":"xpath","selector":".//div[@class='view_body']/div[2]/span"}

不同的版本会给我类似的错误消息.

Different variations give me similar error messages.

driver.find_element_by_css_selector('.view_body>div:nth-child(2)>span')
driver.find_element_by_xpath(".//div[@class='view_body']/div[2]/span")
driver.find_element_by_tag_name('span')

我还尝试了 driver.implicitly_wait(10)无济于事.

推荐答案

要打印文本专辑第1卷[我给您发送的每封信] LP(普通版),您必须诱导 WebDriverWait 用于 visibility_of_element_located()可以使用以下任一定位器策略:

To print the text Album Vol.1 [Every letter I sent you] LP (Normal Edition) you you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • 使用 CSS_SELECTOR :

driver.get('http://www.ktown4u.com/iteminfo?grp_no=231307&goods_no=44363#dt_wrap03')
print(re.split('[*\-]',WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.payment-order-list>ul>li"))).text)[1])

  • 使用 XPATH :

    driver.get('http://www.ktown4u.com/iteminfo?grp_no=231307&goods_no=44363#dt_wrap03')
    print(re.split('[*\-]',WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='payment-order-list']/ul/li"))).text)[1])
    

  • 控制台输出:

  • Console Output:

    Album Vol.1 [Every letter I sent you] LP (Normal Edition)
    

  • 注意:您必须添加以下导入:

  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

  • 这篇关于如何使用Selenium和Python从元素中打印部分文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆