Issue
I have updated my selenium version to selenium 4
and it has a new way of finding elements now as driver.find_elements_by_class()
is now considered deprecated.I'm using this code:
import time
import csv
import requests
import re
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.by import By
def load():
geoblock = webdriver.FirefoxOptions()
geoblock.set_preference("geo.prompt.testing", True)
geoblock.set_preference("geo.prompt.testing.allow", False)
driver = webdriver.Firefox(service=Service(executable_path="/home/thecowmilk/PycharmProjects/webscraping"
"/testing/geckodriver"), options=geoblock)
driver.get(f"file:///home/thecowmilk/PycharmProjects/webscraping/testing/coches.html")
href = driver.find_elements(By.CLASS_NAME, 'mt-CardBasic-titleLink')
print(href)
driver.quit()
def main():
load()
main()
and it is giving me this output:
[<selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="56b32d14-2d66-4ade-82f3-a319538761c9")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="c189e3a5-1fdd-4cec-ae6e-253db47dcc1b")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="0ab1478b-447c-48f2-b828-b2e4624958b4")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="4ecd9727-6e54-4204-80ba-ac8bfc520fc5")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="de507421-849d-4798-8602-2813c4a9e4ff")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="f7f5141e-398b-49e4-a868-8f4c68d685cc")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="c5cb4d7c-e40d-4ae7-9ddb-1daf865d2496")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="256d9d47-e2e5-4603-863a-768366d4c325")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="33867cc0-2dbc-40f0-8de4-b0c86a2ee9ba")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="afc3ba1c-6ef4-40b1-8581-b955e492a4fb")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="ad2f4aec-980a-48c0-bd42-c44912b7fbee")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="eb125aa2-d5e8-46b3-985d-12efe7904243")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="dc286fef-e46c-4d93-8a38-ed1dd5567e21")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="cad921e9-c5df-4d58-898c-fea8d90c9fe2")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="b4783aca-befb-43cc-9dea-04e256a4a9e8")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="574cb402-fb4a-4122-a089-263a64824ab6")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="e0f9ef58-d726-4a20-a5e4-15d6bebf0d03")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="f00f64cd-69ae-49eb-b53c-0dc762d07eec")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="eaa02a23-8217-4d12-8e4b-60d51d3f83bf")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="d10d18cf-237c-40e7-bbf3-cc324b7477e2")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="e5c5251d-6bfd-403f-908d-543bdd7ace1f")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="f9911f61-9fd6-479e-9b87-77ce57bb2dc6")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="991302c8-d154-4496-9356-019f610c2207")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="909340c8-1924-4496-b497-818c3bffd01f")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="d643a221-6946-43bb-8548-647ffa400fbc")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="57da1852-af28-4b16-ac95-090ba129afb7")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="6f34a8b1-43c0-40cf-86f5-14f8b8322ad2")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="edc8a3d5-a944-4082-af34-e5f72adcbc56")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="22cd043d-fbb5-4d58-8c77-dd6e5619c4a0")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="b976bd0e-7828-4d2c-b0c8-35a5a4b9bc12")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="3ef4b4a3-c8a0-41c7-a17b-e265c7dda12f")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="d932cace-2e4c-410f-9c15-a0d0181a591f")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="d6a4974f-239f-4581-ac33-16529780b333")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="adae1ee3-3f2e-4466-96ea-cbf8dc530efc")>, <selenium.webdriver.remote.webelement.WebElement (session="9b29e3f1-5f5d-4533-8ce6-f5d1cf04ad8b", element="28ee0e4d-9e57-43c0-9c44-70c4d020344e")>]
Wonder what am I missing...
Edit: I know that find_elements()
will return a list but I'm expecting something like this:
<a class="mt-CardBasic-titleLink" title="LEXUS IS 300h Hybrid Drive Navi Tecno 4p." href="https://www.coches.net/lexus-is-300h-hybrid-drive-navi-tecno-4p-electrico-hibrido-2014-en-madrid-51582664-covo.aspx" target="_self"></a>
So I can get href
value.
Solution
If you are looking for just one element, simply use find_element()
instead.
After obtaining the element you can get the href
by get_attribute('href')
.
In short,
elem = driver.find_element(By.CLASS_NAME, 'mt-CardBasic-titleLink')
href = elem.get_attribute('href')
Answered By - Shiladitya Bose
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.