簡體   English   中英

使用href查找元素並單擊它。 復制xpath不起作用

[英]Find an element using href and click on it. Copying xpath does not work

我試圖使用python的selenium從本網站上刪除一些信息。

首先,我登錄網站進入頁面 然后,我想點擊“Quickscan”選項卡來獲取一些信息。 然而,這就是我被卡住的地方。 我找不到點擊標簽的方法。

請注意,如果我設法導航到頁面 ,這個問題就會被克服,但是當我登錄時,即使我將這樣的頁面放在我的WebDriver中,我仍然會被重定向到這個頁面

為了到達所需的頁面,我嘗試通過xpath和鏈接找到元素,但它找不到元素。

import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

driver =webdriver.Chrome(executable_path ="mypath")

driver.get("https://vc4a.com/ventures/autocollect/#quickscan-tab")



#find username and password bar
username = driver.find_element_by_id("user_login")
password = driver.find_element_by_id("user_pass")

#Input password and username
username.send_keys("username")
password.send_keys("password")

#click on submit
driver.find_element_by_name("wp-submit").click()
driver.find_element_by_name("rememberme").click()

#try to find element using text in the link
driver.find_elements_by_link_text('#quickscan-tab')[0].click()

#try to find element using xpath from the inspected element
driver.find_element_by_xpath('//*[@id="subnav"]/li[3]/a').click()

我希望能夠打開標簽,以便我可以抓取內容。

當我使用第一個代碼時,它返回以下錯誤:

IndexError: list index out of range

但是,通過檢查頁面我可以看到確實有2個元素帶有“#quickscan-tab”文本,所以我不明白為什么索引0超出范圍。

當我使用第二個代碼時,它返回以下錯誤:

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="subnav"]/li[3]/a"}
  (Session info: chrome=74.0.3729.169)
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Mac OS X 10.14.5 x86_64)

我所做的只是復制xpath。

driver.find_elements_by_link_text('#quickscan-tab')[0] .click() - 這是錯的

鏈接文本不能像這樣工作,您需要創建一個不同的定位器。 嘗試下面的XPath

driver.find_element_by_xpath((//*[@id='quickscan-tab'])[0])

我在該頁面上創建了一個帳戶並嘗試了這個修改過的腳本,它可以工作:

import requests
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

driver = webdriver.Chrome()

driver.get("https://vc4a.com/ventures/autocollect/#quickscan-tab")

#find username and password bar
username = driver.find_element_by_id("user_login")
password = driver.find_element_by_id("user_pass")

#Input password and username
username.send_keys("username")
password.send_keys("password")

#click on submit
driver.find_element_by_name("rememberme").click()
driver.find_element_by_name("wp-submit").click()
time.sleep(10)

#try to find element using text in the link
driver.find_elements_by_link_text('Quickscan')[0].click()

#try to find element using xpath from the inspected element
driver.find_element_by_xpath('//a[text()="Quickscan"]').click()
  1. link_text表示您實際看到的文本。 [快速掃描]
  2. 登錄需要時間,腳本會在創建選項卡之前嘗試找到,從而導致錯誤。
  3. 如果不是登錄延遲,您的xpath將起作用。
  4. 在提交表單之前單擊rememberme。 或者不這樣做,因為selenium會為每次運行啟動一個干凈的會話。

嘗試這個:

scanelements = driver.find_elements_by_xpath('//*[@id='quickscan-tab']')
for elt in scanelements :
   elt.click()
   break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM