1

Hi I am trying to access this element span. I want to Access it in a way that it can be appended to a list. every span has a wordnr="1" or any number. I want every wordnr into list.

This is what the source code of the site looks

<div id="row1" style="top: 1px;"><span wordnr="0" class="highlight">who</span> <span wordnr="1" class="">other</span> <span wordnr="2" class="">even</span> <span wordnr="3" class="">study</span> <span wordnr="4" class="">near</span> <span wordnr="5" class="">put</span> <span wordnr="6" class="">did</span> <span wordnr="7" class="">up</span> <span wordnr="8" class="">small</span> <span wordnr="9" class="">white</span> <span wordnr="10" class="">go</span> <span wordnr="11" class="">call</span> <span wordnr="12" class="">the</span> <span wordnr="13" class="">big</span> <span wordnr="14" class="">she</span> <span wordnr="15" class="">example</span> <span wordnr="16" class="">part</span> <span wordnr="17" class="">those</span> <span wordnr="18" class="">mean</span> <span wordnr="19" class="">plant</span> <span wordnr="20" class="">down</span> <span wordnr="21" class="">just</span> <span wordnr="22" class="">school</span> <span wordnr="23" class="">still</span> <span wordnr="24" class="">only</span> <span wordnr="25" class="">went</span> <span wordnr="26" class="">thing</span> <span wordnr="27" class="">soon</span> <span wordnr="28" class="">make</span> <span wordnr="29" class="">small</span> <span wordnr="30" class="">when</span> <span wordnr="31" class="">at</span> <span wordnr="32" class="">such</span> <span wordnr="33" class="">work</span> <span wordnr="34" class="">together</span> <span wordnr="35" class="">world</span> <span wordnr="36" class="">like</span> <span wordnr="37" class="">want</span> <span wordnr="38" class="">really</span> <span wordnr="39" class="">her</span> <span wordnr="40" class="">show</span> <span wordnr="41" class="">turn</span> <span wordnr="42" class="">such</span> <span wordnr="43" class="">over</span> <span wordnr="44" class="">hard</span> <span wordnr="45" class="">place</span>

Here is my code which does not make sense because I don't know know how to access it or find it. I am using Repl.it so it can be a little confusing looking.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options

list=[]

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(options=chrome_options)


driver.get("https://10fastfingers.com/typing-test/english")

words= driver.find_element_by_xpath("//span[@class='wordnr'/span[0]]").text
0

4 Answers 4

2

1 There is "Accept cookies" button. Click it before getting the text.

2 To get the text use item.get_attribute("innerHTML"). Not sure why item.text does not work for your case. I'll check it later, really interesting.

3 Selenium creates a list for you with my_list = driver.find_elements_by_css_selector("#row1>span[wordnr]") You do not need to create extra list and append value to it. Furthermore, doing so in your case could lead to a list containing inner lists.

4 Use correct explicit waits. visibility_of_all_elements_located won't work because text is only partially visible.

5 Try not to use such identifiers as list. It may be a problem for code readability. Check interesting question here Better Python list Naming Other than "list". My solution

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


chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver', options=chrome_options)


driver.get("https://10fastfingers.com/typing-test/english")
wait = WebDriverWait(driver, 20)

#wait for first element presence
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#CybotCookiebotDialogBodyLevelButtonLevelOptinAllowallSelectionWrapper>#CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll"))).click()

wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "span[wordnr]")))
my_list = driver.find_elements_by_css_selector("#row1>span[wordnr]")
print(len(my_list))
for item in my_list:
    print(item.get_attribute("innerHTML"))

Sample output

385
keep
country
been
their
food
do
...

Prints the whole list (385 words).

Sign up to request clarification or add additional context in comments.

Comments

1

There's a accept all cookies button. you have to click on it. Below is the sample code :

code :

driver.maximize_window()
driver.get("https://10fastfingers.com/typing-test/english")
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[id='CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll']"))).click()
sleep(5)
for name in driver.find_elements(By.XPATH, "//span[@wordnr]"):
    print(name.get_attribute('wordnr'))

Imports :

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

2 Comments

This answer is also better than the accepted one, voting for it.
It's strange that OP has accepted the worst answer among available answers. OP had an issue yesterday, this is super confusing stuff :)
1

I think the following should work.
Wait until the page is loaded, get the list of relevant elements and then extract texts from each element and append it to the list

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(options=chrome_options)
wait = WebDriverWait(driver, 20)

driver.get("https://10fastfingers.com/typing-test/english")
texts_list=[]
#wait for first element presence
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span[wordnr]")))
#wait for the rest elements loaded
time.sleep(1)

elements = driver.find_elements_by_css_selector("span[wordnr]")
for element in elements:
    texts_list.append(element.text)

4 Comments

It's better not to name a custom list list
You are right, thanks! Updated the variable name
At least i'll upvote your answer because I do no agree with the accepted one. That approach is very unstable.
I agree with you as usual :)
0

See if this works:-

list = []
words = driver.find_elements_by_xpath("//div[@id='row1']/span")
for e in words:
    list.append(e.get_attribute("wordnr"))

Comments

Your Answer

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