3

Grab the text of the specified area.

Website: https://www.kobo.com/tw/zh/ebook/NXUCYsE9cD6OWhvtdTqQQQ.

Image:

https://imgur.com/a/qK1uA9L

Code:

BookTitle = driver.find_elements_by_xpath('//p[@class="title product-field"]')
BookTitle[0].getWindowHandle() 

HTML:

<span translate="no">大塊文化</span>
1
  • Your selector has nothing to do with the element, generally you could right click the element on dev tool to copy selector Commented Apr 9, 2019 at 10:23

4 Answers 4

1

To extract the text 大塊文化 from the specified area you need to induce WebDriverWait for the visibility_of_element_located() and you can use the following solution:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument("--disable-extensions")
    options.add_argument('disable-infobars')
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get('https://www.kobo.com/tw/zh/ebook/NXUCYsE9cD6OWhvtdTqQQQ')
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[text()='電子書詳細資料']//following::ul[1]//li/a[@class='description-anchor']/span"))).text)
    driver.quit()
    
  • Console Output:

    大塊文化
    
Sign up to request clarification or add additional context in comments.

Comments

1

You are doing in wrong way :

BookTitle[0].getWindowHandle() not suppose to do anything here

Simply try :

driver.find_element_by_css_selector("a[class='description-anchor']>span").text

1 Comment

a[class='description-anchor'] -> a.description-anchor perhaps more canonical
0

Try the following code.

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

driver.get("https://www.kobo.com/tw/zh/ebook/NXUCYsE9cD6OWhvtdTqQQQ")
element=WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'a.description-anchor span[translate="no"]')))
print(element.text)

Comments

0

You could also use

driver.find_element_by_css_selector('span[translate="no"]')

CSS Selectors should be faster than XPath

EDIT Edited per DebanjanB comment - thank you

5 Comments

Did you test your answer before publishing it? Does your code compiles?? Is your solution Python based as per OP's requirement???
My apologies reagarding the Python solution, I missed the tag Edited to a Python version a yes, it does compile for me
I'm using this very syntax in my work.... However, I'm confused, other answers suggest very similar if not the same solution, how could my answer have compilation errors and theirs not
You are missing the single quotes i.e. '...' around the selector and it should have been driver.find_element_by_css_selector('span[translate="no"]')
Edited and I apologize for this dumb mistake

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.