Why does:
k = driver.find_elements_by_class_name("#mw-content-text > div > p:nth-child(3) > b:nth-child(1)")[0:6]
Work
But
number 6 (aka titles.text) of wiki gives
k = driver.find_elements_by_class_name("#mw-content-text > div > p:nth-child(3) > b:nth-child(1)")[0:titles.text]
Error:
TypeError: slice indices must be integers or None or have an __index__ method
How do I get around this? Why is it doing this
the code
cd = webdriver.chrome()
cd.get('https://en.wikipedia.org/wiki/6')
titles = driver.find_elements_by_class_name("#mw-content-text > div > p:nth-child(3) > b:nth-child(1)")
for title in titles:
print(title.text)
Changes it to int(titles.text) gives print of all elements
According to the comments, it looks like that the value of titles.text
is, first of all, a string (and hence cannot be used as a slice index) and is equal to 1 of 6
which means that you would not be able to directly pass it to int()
for conversion.
If you want to extract that 6
from the string, you first can split the string by of
and then get the last element. Then, you would need to convert it to an integer:
max_pages = int(titles.text.split(" of ")[-1])
k = driver.find_elements_by_class_name("#mw-content-text > div > p:nth-child(3) > b:nth-child(1)")[:max_pages]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.