如何构造XPath以从父ul标签提取URL www.fastreact.co.uk?

时间:2018-07-25 22:30:01

标签: python selenium selenium-webdriver xpath webdriver

您好,我想使用硒<li>www.fastreact.co.uk</li>导航到此确切的供应商URL。但是我被困在这一部分。

vendor_url = browser.find_element_by_xpath('//ul[@class="check-list"]')
#vendor_url = browser.find_element_by_xpath('//ul[@class="check-list"][3]')

如何导航到确切的UL?从源代码中可以看到,UL包含class="check-list ... "的情况有四次。
同样,一旦我设法导航到该节点,如何将硒Web元素输出到人工文本中?

https://www.capterra.com/p/14890/Fastreact/

<h2 class="epsilon"><i class="ss-buildings  icon-lead  zeta"></i>VendorDetails</h2>
<ul class="check-list" >
    <li>Fast React Systems</li>
    <li>www.fastreact.co.uk</li>
    <li>Founded 1999</li>
    <li>United Kingdom</li>
</ul>

2 个答案:

答案 0 :(得分:1)

这是您想要的工作示例:

  

注意:这已在带有chromedriver 2.40的Chrome build 67上进行了测试

driver.get('https://www.capterra.com/p/14890/Fastreact/')

# this will go directly to the check-list item you are wanting
vendor_details = driver.find_element_by_xpath('.//ul[@class="check-list"][2]')
print(vendor_details.text)

# this will go directly to the link you are wanting
vendor_link= driver.find_element_by_xpath('.//ul[@class="check-list"][2]/li[2]')
print(vendor_link.text)

print 语句将显示控制台中元素的文本值。

答案 1 :(得分:1)

根据您要提取供应商URL 的问题,您需要诱使 WebDriverWait 使所需的元素可见,您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument('disable-infobars')
    driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://www.capterra.com/p/14890/Fastreact/")
    print(WebDriverWait(driver, 20).until(expected_conditions.visibility_of_element_located((By.XPATH, "//h2[@class='epsilon' and contains(.,'Vendor Details')]//following::ul[1]//following-sibling::li[2]"))).get_attribute("innerHTML"))
    
  • 控制台输出:

    www.fastreact.co.uk