我正在尝试抓取此页面。在进入页面列表之前,会弹出一个“选择位置”窗口,因此,我试图告诉Selenium单击两个按钮以访问产品列表。
问题是,Selenium无法找到我用来定位这两个按钮的xpath!
这是我的代码:
from selenium import webdriver
driver = webdriver.Chrome("webdriver/chromedriver.exe")
driver.implicitly_wait(30)
driver.get("https://www.indiacashandcarry.com/shop/HomestyleFood")
locationButton = driver.find_element_by_xpath('//*[@id="location-list"]/li[1]/h4/a')
groceriesButton = driver.find_element_by_xpath('//*[@id="price-list-0"]/ul/li[1]')
locationButton.click()
groceriesButton.click()
这是网站: https://www.indiacashandcarry.com/shop/HomestyleFood
我在想这是因为此弹出窗口位于其他类型的框架上,但是我找不到任何iframe索引,所以我有点迷失了。请帮忙!
答案 0 :(得分:2)
您的xpath看起来不错。使用from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option('prefs', {
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True
}
)
driver = webdriver.Chrome("C:\Python\chromedriver",options = chrome_options)
driver.get("http://www.jmtba.or.jp/english/date/2019/?cat=169")
i=2010
while i<=2019:
driver.find_element_by_link_text(str(i)).click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "news_archive")))
results = driver.find_element_by_class_name("news_archive")
rows = results.find_elements_by_tag_name("li")
for row in rows:
# get pdf link
a = row.find_element_by_tag_name("a")
# download pdf
a.click()
i = i + 1
处理动态元素。
Webdriverwait
答案 1 :(得分:1)