未找到Python Selenium路径

未找到Python Selenium路径,python,python-3.x,python-2.7,selenium,selenium-webdriver,Python,Python 3.x,Python 2.7,Selenium,Selenium Webdriver,我已经写了代码 import os from webdriver_manager.chrome import ChromeDriverManager import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.keys import Keys from selenium.webdriver.commo

我已经写了代码

import os
from webdriver_manager.chrome import ChromeDriverManager
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)
url = "https://www.moneycontrol.com/india/stockpricequote/chemicals/tatachemicals/TC"
driver.get(url)
try:
    wait = WebDriverWait(driver, 10)
except Exception:
driver.send_keys(Keys.CONTROL +'Escape')
driver.find_element_by_link_text("Bonus").click()
try:
    wait = WebDriverWait(driver, 5)
except Exception:
    driver.send_keys(Keys.CONTROL +'Escape')
for i in range(0, 50):
    bonus_month = driver.find_element_by_xpath ("//*[@class= 'mctable1.thborder.frtab']/tbody/tr[%s]/td[1]"%(i)) 
    print(bonus_month.text)
    bonus = driver.find_element_by_xpath ("//*[@class= 'mctable1.thborder.frtab']/tbody/tr[%s]/td[1]"%(i)) 
    print(bonus.text)
这给了我错误

no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class= 'mctable1.thborder.frtab']/tbody/tr[0]/td[1]"}
页面上的元素:


我在查找Exbonus和Ratio时出错的地方?

这部分将解决您的定位器问题:

1要查找额外奖金,请使用
css
选择器:
\id\u b>tr>td:n类型(2)

2要查找比率,请同时使用css选择器,类型(3)的td:n

要使用iTrante,请执行以下操作:

#id_b>tr:nth-of-type(x)>td:nth-of-type(3)
其中
x
是行数。 例如,
#id\u b>tr:nth类型(1)>td:nth类型(3)
将为您提供比率为3:5的文本

如果避免使用#id_b,则此定位器将不是唯一的。 我使用的是按css选择器查找元素,而不是范围函数。请尝试以下操作:

rows = driver.find_elements_by_css_selector("#id_b>tr")
for row in rows:
    bonus = row.find_element_by_css_selector("td:nth-of-type(2)").text
    ratio = row.find_element_by_css_selector("td:nth-of-type(3)").text

页面上只有5个这样的元素。我必须单击
查看更多

首先使用
预期条件
中的可单击方法来检查元素是否在给定时间内可单击,以确保其可操作

在奖金链接上执行单击操作后,表格需要一些时间才能完成加载。同时,selenium尝试获取表内容,但获取失败。因此,再次添加wait,等待元素加载,然后使用Xpath获取表并迭代表中的行-

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//ul[@id='corporation_tab']//a[@href='#ca_bonus']")))

driver.find_element_by_link_text("Bonus").click()

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//tbody[@id='id_b']/tr")))

tableRows = driver.find_elements_by_xpath('//tbody[@id="id_b"]/tr')
print(tableRows)
for i in tableRows:
    AnnouncementDate = i.find_element_by_xpath('td[1]').text
    exbonus = i.find_element_by_xpath('td[2]').text
    ratio = i.find_element_by_xpath('td[3]').text

    print(AnnouncementDate + " \t\t " + exbonus + " \t\t " + ratio)
这将返回我的输出-

您将需要以下额外的导入-

from selenium.webdriver.support import expected_conditions as EC

奖金月-页面上的哪个元素?出现错误,因为无法找到此元素,但我添加了驱动程序。通过奖金链接文本(“奖金”)查找奖金元素。单击()可单击奖金链接,页面将提前打开,但未选择数据。是否单击?此奖金和比率只打印空行。行。文本给出“1995年6月19日1995年10月4日3:5“但是奖金和比率打印空白为什么我们不能使用table class和tr td找到?AttributeError:'WebElement'对象没有属性'driver'是的,我删除了这部分,再次检查Hanks,已经到了这一点,但是我们如何区分日期和奖金?@sam根据您的要求更新了答案。不过很简单。是的,我也做了同样的事。谢谢你的回答。css也可以做同样的事。