Felipe
Felipe

Reputation: 43

Selenium script is not getting the text of an element

I'm trying to get the owner of a website through my local whois with this:

link = "ebdicorp.com.br"
service = service.Service('C:\Selenium\chromedriver.exe')
service.start()
capabilities = {'chrome.binary': 'C:\Selenium\chromedriver.exe'}
driver = webdriver.Remote(service.service_url, capabilities)
driver.get('https://registro.br/2/whois?qr=&c');
time.sleep(5)

input_site = driver.find_element_by_id("whois")
input_site.send_keys(link)
driver.find_element_by_id("captchaBtn").click()
company = driver.find_element_by_class_name("col-md-9").text

print(company)

driver.quit()

The problem is: I get the element with

company = driver.find_element_by_class_name("col-md-9")

which returns <selenium.webdriver.remote.webelement.WebElement (session="eae9915a3cc3f2f4690eec4a0019982d", element="0.5670714519595477-1")>

But there isn't any text when I try with .text. Where am I messing up?

Upvotes: 1

Views: 1313

Answers (2)

Guy
Guy

Reputation: 50809

There are 20 elements with class col-md-9. The first one, which you get, has no text. You can use xpath to locate the row you are looking for by the text and from there get the value.

For example, for the "Titular" row use

driver.find_element_by_xpath("//*[label='Titular:']/following-sibling::*[1]").text

Which will give you Studio Crazy Suporte S/C Ltda Me

Upvotes: 2

undetected Selenium
undetected Selenium

Reputation: 193058

As per the website https://registro.br/2/whois?qr=&c once you search with the text ebdicorp.com.br to extract any of the values you can write a function which will take the <label> name as follows:

def extract_value(myString):
    driver.find_element_by_xpath("//label[.='" + myString + "']//following::div[1]").get_attribute("innerHTML")

Now you can call the function extract_value() with any of the <label> texts to extract its value as follows:

extract_value("Titular:")
# or
extract_value("Contato do Titular:")
# or
extract_value("Contato Administrativo:")

Upvotes: 1

Related Questions