Home > Software design >  Selenium CSS Selector get only first element while the inspector show 4
Selenium CSS Selector get only first element while the inspector show 4

Time:03-21

I'm trying to automize Linguee dictionary using Selenium.

Search of de

For instance, in the image I would like to get an array with [de, para, por, con]. In order to get this, I wrote the following code

from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.by import By

s=Service(r"C:\Users\Usuario\python\geckodriver.exe")
driver = webdriver.Firefox(service=s)

driver.get('https://www.linguee.fr/frances-espanol/')
consent_button=driver.find_element(By.CSS_SELECTOR, "div[class='sn-b-def sn-blue']")
consent_button.click()


search_input=driver.find_element(By.CSS_SELECTOR, "input[id='queryinput']")
search_input.send_keys("de")

buttonSearch=driver.find_element(By.CSS_SELECTOR, "button[alt='Rechercher dans le dictionnaire français-espagnol']")
buttonSearch.click()

wortType=driver.find_element(By.CSS_SELECTOR, "span[class='tag_wordtype']").text
print( wortType)

translations=driver.find_element(By.CSS_SELECTOR, "div[class='isMainTerm'] a[class='dictLink']").text
print(translations)

The code work correctly, but it returns only the first translations ("en" in the image) while in the browser console I get 4. So, I have been reading and trying differents ways of fix the problem, like change the CSS_SELECTOR

translations=driver.find_element(By.CSS_SELECTOR,"div[class='isMainTerm'] div[class='exact'] div[class='lemma featured'] div[class='lemma_content'] div[gid=0] div[class='translation_lines'] div[class='translation sortablemg featured'] a")
translations=driver.find_element(By.CSS_SELECTOR, "div[class='isMainTerm'] a[class='dictLink']").text
translations=driver.find_element(By.CSS_SELECTOR, "div[class='isMainTerm'] a[class='dictLink']")
translations=driver.find_element(By.CSS_SELECTOR,"div.isMainTerm div.lemma_content a.dictLink").text
translations=driver.find_element(By.CSS_SELECTOR, "div[class='isMainTerm']> a[class='dictLink featured']").text

I have also used XPath but also returns only one. I have read differents post in stackoverflow with similar problems like this enter link description here, but the problem persists.

Sorry this is long-winded, but can someone guide me to why it's doing this and what my options are?

CodePudding user response:

This is how find_element method works.
It returns the first matching element on the DOM it finds.
So in case you are passing it locator matching several elements on the page the first matching element on the DOM will be returned.
In case you want to get all the elements matching that locator find_elements method should be used instead.
So, in case you want to get all the texts in elements matching div[class='isMainTerm'] a[class='dictLink'] CSS Selector instead of

translations=driver.find_element(By.CSS_SELECTOR, "div[class='isMainTerm'] a[class='dictLink']").text
print(translations)

You can use

translations = driver.find_elements(By.CSS_SELECTOR, "div[class='isMainTerm'] a[class='dictLink']")
for translation in translations:
    print(translation.text)

BTW, it is only this CSS Selector locator div[class='isMainTerm'] a[class='dictLink featured'] is matching 4 elements as you mentioned and it looks like this locator you should use here while div[class='isMainTerm'] a[class='dictLink'] locator is matching 81 elements! on that page.

  • Related