Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从表中获取TD元素_Python_Python 3.x_Python 2.7_Selenium - Fatal编程技术网

Python 从表中获取TD元素

Python 从表中获取TD元素,python,python-3.x,python-2.7,selenium,Python,Python 3.x,Python 2.7,Selenium,我正在从一个网页上获取债务与股权比率的价值 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 impo

我正在从一个网页上获取债务与股权比率的价值

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)值,然后单击Standalone并获取相同的值。

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

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

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

可以获得合并的值,但在单机版中,其输出为:Error Error Error Error resolved the Error,第二次没有比率单击。谢谢你的帮助。你能解释一下这行代码吗?bro-driver。通过xpath(“/*[@id=\“consolidated\”]/genderant::div[text()=\“债务转股权(x)\”]/parent::td/后续兄弟姐妹::td[1]”)找到元素。print(“consolidated:“+elem.text”)很想理解。马克:事实上,如果我解释的话,它会变得很长。更好,我会给你资源,你可以看到和阅读,以更好地了解XPath。首先参考youtube视频,并从中阅读有关xpath轴的内容。如果你需要更多的解释,一定要告诉我,别忘了把它标为答案。:)当然可以,兄弟。非常感谢。