2

Before asking, I find answer through google for 2hours. But there's no answer for me.

I use selenium with python

I apply below q/a answer to my code but nothing text printed.

XPath query to get nth instance of an element

What I want to get is "Can't select"

<li data-index="5" date="20190328" class="day dimmed">
    <a href="#" onclick="return false;">
        <span class="dayweek">Tuesday</span>
        <span class="day">28</span>
        <span class="sreader">Can't select</span>
    </a>
</li>

I use xpath because I need to repeat

I should do this. The HTML code above is a simple change

day_lists = driver.find_elements_by_xpath('//li')

Nothing printed and there's no error

for day_list in day_lists:
    print(day_list.find_element_by_xpath('//span[@class="sreader"]').text)

++++ 2019/3/24/16:45(+09:00)

When I test with below code

print(day_list.find_element_by_xpath('.//span[@class="sreader"]/text()'))

Error comes out. Why there's no such element?

selenium.common.exceptions.NoSuchElementException: 
Message: no such element: Unable to locate element: 
{"method":"xpath","selector":".//span[@class="sreader"]/text()"}

4 Answers 4

1

If nothing printed and there's no error then required text might be hidden or not generated yet.

For first case you might need to use get_attribute('textContent'):

day_lists = driver.find_elements_by_tag_name('li')
for day_list in day_lists:
    print(day_list.find_element_by_xpath('.//span[@class="sreader"]').get_attribute('textContent'))

For second case:

from selenium.webdriver.support.ui import WebDriverWait as wait

day_lists = driver.find_elements_by_tag_name('li')
for day_list in day_lists:
    print(wait(driver, 10).until(lambda driver: day_list.find_element_by_xpath('.//span[@class="sreader"]').text)

Note that in both cases you need to add leading dot in XPath:

'//span' --> './/span'
Sign up to request clarification or add additional context in comments.

4 Comments

I test <a href code if list did not loaded, but working well. And What you say case1, case2 also result nothing,
hmm... for me those span nodes really appears as empty: <span class="sreader"></span>. I see no text. It seem that you need to select (click on) date to generate "선택불가" text...
I always check after click... Thank you
Finally this worked! thanks!!
0

To print the desired text e.g. 선택불가 from the <span> tag you can write a function as follows:

def print_text(my_date):
        print(driver.find_element_by_xpath("//li[@class='day dimmed'][@date='" +my_date+ "']//span[@class='sreader']").get_attribute("innerHTML"))

Now you can call the function with any date as follows:

print_text("20190328")

2 Comments

I want to use day_list. day_list.find_element_by_xpath('//span[@class="sreader"]').get_attribute('innerHTML') result same
day_list.find_element_by_xpath('///li[@date="'+day_list.get_attribute('date')+'"]//span[@class="sreader"]').get_attribute('innerHTML') this also result same. nothing printed
0

Here are the solutions. If this does not work then I predict the element might present either in separate window or frame.

CSS: li[class='day dimmed'][date='20190328'] .sreader enter image description here

xpath:

enter image description here

To check if there are multiple windows use below

print(len(driver.window_handles))

To check if there are multiple frames use below

print(len(driver.find_elements_by_css_selector('iframe')))

3 Comments

day_list.find_element_by_xpath('.//a').send_keys(Keys.ENTER) act, But Why print(day_list.find_element_by_xpath('//span[@class="sreader"]').text) print nothing? I also checked UTF8, that's not problem
And I can't use driver. Why I use day_list is I use this stie "cgv.co.kr/ticket". My question is nothing to do with this all code. Because I changed some code. So Anyway I should use day_list.
.get_attribute('innerText') result same, nothing printed
0

Try the following options:

day_lists=WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, '//li[@class="day dimmed"]')))
for day_list in day_lists:
    print(day_list.find_element_by_xpath('//span[@class="sreader"]').text)

OR

day_lists=WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, '//li[@class="day dimmed"]')))
for day_list in day_lists:
   print(driver.execute_script("return arguments[0].innerHTML;", day_list.find_element_by_xpath('//span[@class="sreader"]')))

Please note that you need following imports as well.

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

1 Comment

both print nothing. I attached some code at main text. Can you check them?

Your Answer

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