【问题标题】:Get TD element from Table从表中获取 TD 元素
【发布时间】:2021-07-06 01:57:40
【问题描述】:

我正在从网页中获取债务对股权比率的值。

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument('--ignore-certificate-errors')
options.page_load_strategy = 'eager'    
driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)
wait = WebDriverWait(driver, 20)
driver.get("https://www.moneycontrol.com")

inputElement=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#form_topsearch>.txtsrchbox.FL'))) 
inputElement.send_keys('3IINFOTECH',Keys.ENTER)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#proceed-button'))).click()

driver.implicitly_wait(30)
print("---")
try:
    driver.find_element_by_link_text("Financials").click()
    driver.find_element_by_link_text("Ratios").click()
    driver.find_element_by_link_text("Leverage Ratios").click()
    elem = driver.find_element_by_xpath("//*[@id='body']/table//thead/tbody/tr[1]/td[2]")
    print(elem.text)
except e:
    print(e)

我无法获取最新(2020 年 3 月)的债务权益 (x) 值,然后单击独立并获取相同的值。

【问题讨论】:

    标签: python python-3.x python-2.7 selenium


    【解决方案1】:

    请找到上述问题的工作代码。根据您的需要增强代码。

    import time
    
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    options = Options()
    options.add_argument('--ignore-certificate-errors')
    options.add_argument('--start-maximized')
    options.page_load_strategy = 'eager'
    driver = webdriver.Chrome(options=options)
    wait = WebDriverWait(driver, 20)
    driver.get("https://www.moneycontrol.com")
    
    inputElement = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#form_topsearch>.txtsrchbox.FL')))
    inputElement.send_keys('3IINFOTECH', Keys.ENTER)
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#proceed-button'))).click()
    
    driver.implicitly_wait(30)
    print("---")
    try:
        driver.find_element_by_link_text("Financials").click()
        driver.find_element_by_link_text("Ratios").click()
        driver.find_element_by_link_text("Leverage Ratios").click()
        time.sleep(2)
        elem = driver.find_element_by_xpath(
            "//*[@id=\"consolidated\"]/descendant::div[text()=\"Debt to Equity (x)\"]/parent::td/following-sibling::td[1]")
        print("Consolidated : " + elem.text)
    
        driver.find_element_by_link_text("Standalone").click()
        driver.find_element_by_link_text("Ratios").click()
        driver.find_element_by_link_text("Leverage Ratios").click()
        time.sleep(5)
        elem = driver.find_element_by_xpath(
            "//*[@id=\"standalone\"]/descendant::div[text()=\"Debt to Equity (x)\"]/parent::td/following-sibling::td[1]")
        print("Standalone : " + elem.text)
    except:
        print("Error Error Error")
    

    如果问题解决,请将其标记为答案。

    【讨论】:

    • 可以获得合并的值,但在独立时它给出的输出为:错误错误错误
    • 解决了错误,第二次没有比例点击。谢谢你的帮助。你能解释一下这一行吗兄弟 - driver.find_element_by_xpath("//*[@id=\"consolidated\"]/descendant::div[text()=\"Debt to Equity (x)\"]/parent ::td/following-sibling::td[1]") print("合并:" + elem.text)
    • 很想了解。也会标记答案
    • 其实如果我解释它会变得很长。更好的是我会给你资源,你可以看到和阅读以更好地理解 Xpaths。首先参考youtube.com/watch?v=3uktjWgKrtI&t=1873s youtube 视频,并从w3schools.com/xml/xpath_axes.asp 阅读有关xpath 轴的信息。如果您需要更多解释,请告诉我,不要忘记将其标记为答案。 :)
    猜你喜欢
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多