Py学习  »  Python

Python Selenium——访问嵌套div中的文本

David Bolton • 6 年前 • 2545 次点击  

尝试访问嵌套的某些文本属性。对大多数元素都没有问题,只是没能得到某些元素。

一个变量=Optomi,另一个变量=Detroit,Michigan,United States(尽管我觉得一旦我有了第一个变量,我就可以很容易地得到第二个)

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> 

我只是想把文本值“Optomi”拉出来。

我试过:

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

和

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"))

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/56409
文章 [ 2 ]  |  最新文章 6 年前
DebanjanB
Reply   •   1 楼
DebanjanB    6 年前

抽出文字 奥托米 你需要诱导 网络驱动器 visibility_of_element_located() 你可以使用下列任一方法 Locator Strategies

  • 使用 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)
    
  • 使用 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)
    
  • 注意

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
MoriartyPy
Reply   •   2 楼
MoriartyPy    6 年前

尝试使用CC7052所说的:

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

然后可能会用。分到底特律

cname.split(',')[0]