我正在尝试使用硒/铬驱动程序从宏观趋势自动下载文件(当我得到这个工作时,phantomJS(。我刚刚意识到我正在单击的下载按钮在 iframe 中,所以我尝试切换到 iframe,然后单击该按钮,但我仍然收到 NoSuchElementException。另外,驱动程序是否有任何内置功能,我可以用来不发送垃圾邮件,也不必使用 time.sleep?
class get_price_data:
def __init__(self, name):
price_driver.find_element_by_xpath(r'//*[@id="jqxInput"]').send_keys(name)
while(price_driver.current_url=='https://www.macrotrends.net/stocks/charts/AMZN/amazon/stock-price-history'):
price_driver.find_element_by_xpath(r'//*[@id="jqxInput"]').send_keys(u'ue007')
time.sleep(5)
price_driver.switch_to_frame('chart_iframe')
time.sleep(5)
price_driver.find_element_by_class_name(r'dataExport chart_buttons btn btn-danger btn-xs')
price_driver.click()
特斯拉=get_price_data('TSLA'(
单击iframe内的Download Data
。
诱导WebDriverWait()
并等待frame_to_be_available_and_switch_to_it
((
诱导WebDriverWait()
并等待element_to_be_clickable()
和以下 css 选择器。
WebDriverWait(price_driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"chart_iframe")))
WebDriverWait(price_driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,".dataExport.chart_buttons.btn.btn-danger.btn-xs"))).click()
或 在下面使用。
WebDriverWait(price_driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"chart_iframe")))
WebDriverWait(price_driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(.,'Download Data')]"))).click()
您需要导入以下库
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
链接以了解 WebDriverWait
您的选择器在这里不起作用...
尝试替换它:
price_driver.find_element_by_class_name(r'dataExport chart_buttons btn btn-danger btn-xs')
有了这个:
price_driver.find_element_by_class_name('div>button.dataExport')