0

here is the website link :

https://www.milanuncios.com/anuncios/?fromSearch=1&fromSuggester=0&suggestionUsed=0

click on button called Llamar to see the the div

I've been trying to get the text of div.

To do this, I have to find this div element.

this is how the div element looks like in the browser :

enter image description here

#Html Code

<div class="telefonos">699771517</div>

I tried this two lines of python code:

#Python Code

TeleFonos = driver.find_element_by_class_name("telefonos")
print(TeleFonos.text)

but im always getting this error

#Error

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".telefonos"}

Do you know what I am doing wrong?

Dzeko
  • 27
  • 6

2 Answers2

1

Your element is in the iframe so you need to switch into that iframe and then search. To do that use the below code -

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

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 60)
driver.get('https://www.milanuncios.com/anuncios/?fromSearch=1&fromSuggester=0&suggestionUsed=0')

wait.until(EC.presence_of_element_located((By.XPATH, "//button[@data-testid=\"TcfAccept\"]"))).click()

try:
    All_Phone = wait.until(
        EC.visibility_of_all_elements_located((By.XPATH, "//a[contains(@onclick,\"phoneContactFromAdList\")]")))
    for phone in All_Phone:
        phone.click()
        iframe = wait.until(EC.presence_of_element_located((By.ID, "ifrw")))
        driver.switch_to.frame(iframe)
        print(wait.until(EC.presence_of_element_located((By.CLASS_NAME, "telefonos"))).text)
        driver.switch_to.default_content()
        driver.find_element_by_xpath("//a[contains(text(),\"Cerrar\")]").click()
except:
    pass

If it resolves your issue then please mark it as answer.

Swaroop Humane
  • 1,770
  • 1
  • 7
  • 17
0

Could it be that the element is not loaded at the time that Selenium is looking on the page?

I recommend looking at the suggestion here on using Selenium wait.

https://stackoverflow.com/a/27112797/4352317

It might help triage the issue if the website in question is shared.

Joe Thor
  • 1,164
  • 1
  • 11
  • 19
  • i have tried driver.implicitly_wait(30) but still the same error – Dzeko Apr 29 '21 at 02:35
  • i edit my Question to add website link i'll be greateful if you check it out – Dzeko Apr 29 '21 at 02:35
  • Looking back at the code, you are trying to access potentially a Psuedo Element because the ::before is included. This makes it difficult to access via DOM. Here is a Stack Overflow answer which may help: https://stackoverflow.com/a/59706267/4352317 – Joe Thor Apr 29 '21 at 02:39
  • I recommend selecting the "Llamar" button by using class="byCall" and then passing it a .click() method. Then wait. Then, you can access the div with the class 'telefonos' – Joe Thor Apr 29 '21 at 02:43
  • thats what i did ToCallBtns = driver.find_elements_by_class_name("tracking-desktop-call-btn") for x in range(len(ToCallBtns)): ToCallBtns[x].click() time.sleep(5) TeleFonos = driver.find_element_by_class_name("telefonos") print(TeleFonos.text) – Dzeko Apr 29 '21 at 02:51
  • @JoeThor that element is in the `iframe`. He has to switch the `iframe` first and then search for that element. The same mentioned in my answer. – Swaroop Humane Apr 29 '21 at 03:13