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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

i tried your code but this error keep showing Undefiened variable wait
There was some mistake, I have updated my code now please try again.
the wait 1 and 2 what they refer to ?
Is that code working and your problem solved? Read about Explicit wait in selenium.
its not working because ithe code is not printing telefonos.text always. it printed it only from few elements. even all the elements is the same. i dont know why
|
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.

6 Comments

i have tried driver.implicitly_wait(30) but still the same error
i edit my Question to add website link i'll be greateful if you check it out
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: stackoverflow.com/a/59706267/4352317
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'
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)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.