0

Trying to access certain text properties that are nested. Have had no trouble with most elements, but just haven't been able to get a certain few.

I'm trying to set: one variable = Optomi, another variable = Detroit, Michigan, United States (Although I feel like once I have the first variable, I can get the second fairly easy)

Full Path

<a data-control-name="company_link" href="/company/optomi/life/" id="ember2228" class="jobs-details-top-card__company-url ember-view"> Optomi </a> 

I'm trying to just pull out the text value "Optomi".

I've tried:

cname = self.driver.find_elements_by_xpath("//a[@class='jobs-details-top-card__company-url ember-view']/a")
print(cname)

and

cname = self.driver.find_elements_by_css_selector(
          "div.jobs-details-top-card__company-info t-14 t-black--light t-normal mt1")
print(cname)
print(cname.get_attribute("text"))

Any tips?

Guy
  • 46,488
  • 10
  • 44
  • 88

2 Answers2

0

Try using what CC7052 said:

cname = self.driver.find_element_by_xpath("//a[@class='jobs-details-top-ard__company-url ember-view']").text

and then maybe use .split for getting Detroit

cname.split(',')[0]
MoriartyPy
  • 21
  • 5
  • Thanks for the help! I tried it, but it just returns an error message: ``` selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@class='jobs-details-top-ard__company-url ember-view']"} ``` – David Bolton Feb 25 '20 at 17:25
0

To pull out the text Optomi you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.jobs-details-top-card__company-url.ember-view[data-control-name='company_link'][href*='optomi']"))).text)
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@class='jobs-details-top-card__company-url ember-view' and @data-control-name='company_link'][contains(@href,'optomi')]"))).text)
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352