Code trials:
driver.get(url)
cards = driver.find_elements_by_class_name("job-cardstyle__JobCardComponent-sc-1mbmxes-0")
for card in cards:
data = card.get_attribute('text')
print(data)
driver.close()
driver.quit()
The "cards" is returning selenium webelement and I am not able to extract the text from it by for loop.
CodePudding user response:
- Check your webelement path whether correctly mentioned or not
- get the text from element
- Print it
CodePudding user response:
Issue is at this line
data = card.get_attribute('text')
You can do the following:
Use
.text
for card in cards: data = card.text print(data)
Use
innerText
for card in cards: data = card.get_attribute('innerText') print(data)
Also, as per the comment above, you should print cards list length to debug it better.
print(len(cards))
so if it has something in it or not.
CodePudding user response:
Instead of get_attribute('text')
you need to use the text
attribute as follows:
data = card.text
Solution
To locate the visible elements you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use the following solution:
driver.get(url)
cards = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "job-cardstyle__JobCardComponent-sc-1mbmxes-0")))
for card in cards:
data = card.text
print(data)
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Outro
In a single line you can use List Comprehension as follows:
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "job-cardstyle__JobCardComponent-sc-1mbmxes-0")))])