How to print the partial text from an element using Selenium and Python

后端 未结 2 585
栀梦
栀梦 2021-01-27 12:01

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 t

相关标签:
2条回答
  • 2021-01-27 12:24

    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:

    • Using 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:

      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
      
    0 讨论(0)
  • 2021-01-27 12:35

    With this 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)"
    
    0 讨论(0)
提交回复
热议问题