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 Answers4

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'
JaSON
  • 4,843
  • 2
  • 8
  • 15
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")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I want to use **day_list**. `day_list.find_element_by_xpath('//span[@class="sreader"]').get_attribute('innerHTML')` result same –  Mar 23 '19 at 14:52
  • `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 –  Mar 23 '19 at 15:03
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')))
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • `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 –  Mar 23 '19 at 18:21
  • And I can't use `driver.` Why I use day_list is I use this stie "http://www.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.` –  Mar 23 '19 at 18:25
  • `.get_attribute('innerText')` result same, nothing printed –  Mar 24 '19 at 07:28
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
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • both print nothing. I attached some code at main text. Can you check them? –  Mar 24 '19 at 07:49