Python 未找到硒路径
Python Selenium Path not found
代码我写好了
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 要查找 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
。
首先使用 expected conditions
中的可点击方法来检查元素在给定时间内是否可点击,以确保其可操作。
一旦对奖金 link 执行了点击操作,table 需要一些时间才能完成加载。与此同时,selenium 尝试获取 table 内容但未能获取。因此,再次添加等待元素加载,然后使用 Xpath 获取 table 并遍历 table 的行。 -
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)
这个returns我输出-
您将需要以下额外导入 -
from selenium.webdriver.support import expected_conditions as EC
代码我写好了
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 要查找 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
。
首先使用 expected conditions
中的可点击方法来检查元素在给定时间内是否可点击,以确保其可操作。
一旦对奖金 link 执行了点击操作,table 需要一些时间才能完成加载。与此同时,selenium 尝试获取 table 内容但未能获取。因此,再次添加等待元素加载,然后使用 Xpath 获取 table 并遍历 table 的行。 -
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)
这个returns我输出-
您将需要以下额外导入 -
from selenium.webdriver.support import expected_conditions as EC