'How to find element with selenium on python?
import os
import selenium
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get('https://www.skysports.com/champions-league-fixtures')
time.sleep(7) #So page loads completely
teamnames = browser.find_element_by_tag("span")
print(teamnames.text)
seems find_element attribute is changed on selenium :/ i also want to find all <img on another local website ( images url ) , appreciate if you can help.
Solution 1:[1]
Replace teamnames = browser.find_element_by_tag("span")
with teamnames = browser.find_element_by_tag_name("span")
Solution 2:[2]
Try to find elements instead of element, because in DOM Tags are always considered multiple.
Example:
browser.find_elements_by_tag_name('span')
Also, not that it will return a list of elements you need to traverse to access properties further.
Solution 3:[3]
Seems selenium made some changes in new version:
from selenium.webdriver.common.by import By
browser = webdriver.Firefox()
browser.get('url')
browser.find_element(by=By.CSS_SELECTOR, value='')
You can also use : By.ID - By.NAME - By.XPATH - By.LINK_TEXT - By.PARTIAL_LINK_TEXT - By.TAG_NAME - By.CLASS_NAME - By.CSS_SELECTOR
I used these in Python 3.10 and now its working just fine
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Shivam Baghla |
Solution 3 | MahdiST |