无法使用selenium python定位元素

2024-09-28 23:18:42 发布

您现在位置:Python中文网/ 问答频道 /正文

对不起,如果我的问题格式是错误的,因为我是新的编程和StackOverFlow。我无法使用类或任何其他方法找到下载按钮。目前正在尝试使用xpath解决问题,但我听说xpath不是可靠的方法。我正试图从wikiloc网站上下载特定位置内的所有轨道路径。登录时可能会出现问题,因为我可能在抓取时未登录。顺便说一句,我主要使用谷歌colab。我的代码如下:

driver.get('https://www.wikiloc.com/wikiloc/start.do')
driver.find_element_by_name('email').send_keys('testacount852')
driver.find_element_by_name('password').send_keys('49Vchu4njDrrT7.')
driver.find_element_by_id('submit-button').click()
time.sleep(10)
driver.get("https://www.wikiloc.com/trails/outdoor/france/brittany")
links = []
temp_links = []
names = []
temp_names = []
for _ in range(4):
  temp = driver.find_elements_by_css_selector('a.trail-title.dont-break-out')
  temp_links = [x.get_attribute('href') for x in temp]
  temp_names = [x.text for x in temp]
  links = links+temp_links
  names = names+temp_names
  try:
    driver.find_element_by_class_name('next').click()
    time.sleep(2)
  except:
    break
for link in links:
  driver.get(link)
  driver.find_element_by_id('download-button').click()
  time.sleep(4)
  driver.find_element_by_class_name('active').click()
  time.sleep(4)
  driver.find_element_by_xpath("/html/body/main/div/div[2]/div/div[1]/div/div[2]/div/div[2]/form/div[2]/input").click()
  time.sleep(4)

df['Names'] = pd.Series(names)
df['Links'] = pd.Series(links)
df.to_csv('test1.csv')
driver.close()

这是指向我的凭据的链接 [1] :https://i.stack.imgur.com/HZ6bZ.png


Tags: namedivforgetbytimenamesdriver
3条回答

youtube上有一个非常好的教程(这个家伙教你如何使用这个非常有趣的饼干点击游戏);它具有try/except功能,下面是我访问selenium项目的转到。我能够让它与下面显示的代码工作,希望这有助于

from selenium import webdriver
from time import sleep
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Chrome()

driver.get('https://www.wikiloc.com/wikiloc/start.do')
driver.find_element_by_name('email').send_keys('testacount852')
driver.find_element_by_name('password').send_keys('49Vchu4njDrrT7.')
driver.find_element_by_id('submit-button').click()
time.sleep(10)
driver.get("https://www.wikiloc.com/trails/outdoor/france/brittany")
links = []
temp_links = []
names = []
temp_names = []
for _ in range(4):
  temp = driver.find_elements_by_css_selector('a.trail-title.dont-break-out')
  temp_links = [x.get_attribute('href') for x in temp]
  temp_names = [x.text for x in temp]
  links = links+temp_links
  names = names+temp_names
  try:
    driver.find_element_by_class_name('next').click()
    time.sleep(2)
  except:
    break
for link in links:
    driver.get(link)
    try:
        downloadbutton = WebDriverWait(driver,15).until(EC.presence_of_element_located((By.CSS_SELECTOR,"#download-button")))
        downloadbutton.click()
    except:
        print("Couldn't find the download button")
        pass
    time.sleep(4)
    driver.find_element_by_class_name('active').click()
    time.sleep(4)
    driver.find_element_by_xpath("/html/body/main/div/div[2]/div/div[1]/div/div[2]/div/div[2]/form/div[2]/input").click()
    time.sleep(4)

df['Names'] = pd.Series(names)
df['Links'] = pd.Series(links)
df.to_csv('test1.csv')
driver.close()

问题2:

  1. 单击下载按钮后,是否没有类名为“active”的webelement

  2. 通过这个xpath,您试图在哪个页面“/html/body/main/div/div[2]/div/div[1]/div/div[2]/div/div[2]/form/div[2]/input”中找到什么? ===========================================================================

更新:

driver.find_element_by_xpath(".//li/a[text() ='File']").click()
driver.find_element_by_xpath(".//input[@id='btn-download-file']").click()

好吧,我找到问题了。是验证码破坏了它。我无法登录,下载按钮(xpath)在我登录之前不可用。感谢各位特别帮助我@ShawnRamirez指出验证码

相关问题 更多 >