简体   繁体   English

如何使用 Selenium 和 Python 从元素打印部分文本

[英]How to print the partial text from an element using Selenium and 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.我正在尝试从http://www.ktown4u.com/iteminfo?grp_no=231307&goods_no=44363#dt_wrap01在描述选项卡下获取文本“Album Vol.1 [Every letter I sent you] LP (Normal Edition)”,但是我不断收到此错误消息。

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')

I've also tried driver.implicitly_wait(10) to no avail.我也试过driver.implicitly_wait(10)无济于事。

With this xpath:使用此 xpath:

'//div[@id="contents"]/div/div/div[2]/span'

Test in dev tools:在开发工具中测试:

$x('//div[@id="contents"]/div/div/div[2]/span')[0].textContent
"Album Vol.1 [Every letter I sent you] LP (Normal Edition)"

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 :要打印文本Album Vol.1 [Every letter I sent you] LP (Normal Edition) ,您必须为visibility_of_element_located()诱导WebDriverWait ,并且您可以使用以下任一Locator Strategies

  • Using CSS_SELECTOR :使用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])
  • Using XPATH :使用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:控制台 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

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

相关问题 如何使用 Selenium 和 Python 从文本节点检索部分文本 - How to retrieve partial text from a text node using Selenium and Python 使用 Selenium (Python) 获取具有部分字符串匹配的元素文本 - Get element text with a partial string match using Selenium (Python) 如何使用selenium webdriver python中的span中的定位器打印文本? - How to print the text using a locator from the span in selenium webdriver python? Selenium Python查找元素部分链接文本 - Selenium Python find element partial link text 如何使用 Selenium WebDriver 和 Python 从表中获取元素(文本) - How to get element(text) from a table using Selenium WebDriver with Python 如何使用 selenium python 使用 href 的部分文本进行迭代 - How to iterate using partial text of href using selenium python 如何使用 Selenium 和 Python 打印出在分隔行中附加有计数器的所有元素文本 - How to print out all the element text appended with a counter in separated lines using Selenium and Python 如何使用 Selenium 和 Python 从下拉菜单中选择 select 选项 - How to select an option from a dropdown menu through partial text using Selenium and Python 使用Selenium和python按部分ID选择元素? - Choose element by partial ID using Selenium with python? 如何通过 Python - selenium 中的部分文本 select - how to select by a partial text in Python - selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM