【问题标题】:Python Selenium Path not found找不到 Python Selenium 路径
【发布时间】:2021-07-12 15:55:51
【问题描述】:

我已经写好了代码

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 时哪里出错了?

【问题讨论】:

  • bonus_month - 页面上的哪个元素?
  • 因为找不到这个元素而出错
  • 但我添加了 driver.find_element_by_link_text("Bonus").click() 以点击 Bonus 链接,页面正在打开但未选择数据。
  • 点击成功了吗?

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


【解决方案1】:

首先使用expected conditions 中的可点击方法检查该元素在给定时间内是否可点击,以确保其可操作。

在奖励链接上执行点击操作后,表格需要一些时间才能完成加载。与此同时,selenium 尝试获取表格内容但未能获取。所以再次添加等待元素加载,然后使用 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

【讨论】:

  • 谢谢,已经到了这一步,但是我们如何区分日期和奖金?
  • @sam 根据您的要求更新了答案。不过这很容易。
【解决方案2】:

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

1 要查找 Ex-Bonus,请使用 css 选择器:#id_b>tr>td:nth-of-type(2)

2 要找到比率,也可以使用 css 选择器,#id_b>tr>td:nth-of-type(3)

反复使用:

#id_b>tr:nth-of-type(x)>td:nth-of-type(3)

其中x 是行数。 例如,#id_b>tr:nth-of-type(1)>td:nth-of-type(3) 将为您提供比例 3:5

的文本

如果您避免避免使用#id_b,则此定位器将不是唯一的。 我会使用 find_elements_by_css_selector 而不是范围函数。请尝试以下操作:

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 个此类元素。我必须点击See More

【讨论】:

  • 这个奖金和比率只打印空白行。
  • row.text 给出“1995 年 6 月 19 日 1995 年 10 月 4 日 3:5”,但奖金和比例打印空白
  • 为什么我们不能使用table class定位然后tr td?
  • AttributeError: 'WebElement' 对象没有属性 'driver'
  • 是的,我删除了这部分,再检查一遍
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 2018-05-09
  • 2020-10-21
  • 1970-01-01
相关资源
最近更新 更多