簡體   English   中英

為什么我的 xpath 不能與 selenium 一起使用?

[英]Why is my xpath not working with selenium?

我需要編寫一個腳本,根據用戶在我的網站中的輸入,它連接到此頁面以提交號碼並下載文件。 問題是,xpath 根本不工作。 我究竟做錯了什么?

這是觀點(我刪除了與問題無關的部分):

from django.shortcuts import render, HttpResponse
import requests
from django.views.generic import FormView


from time import sleep
from selenium import webdriver

class ConstanciaInscripcion(FormView):

    def get(self, request):
       return render(request, 'app/constancia-inscripcion.html')
    
    def post(self,request):

        form = MonotributoForm(request.POST)
        email = request.POST['Email']
        cuit_r = int(request.POST["CUIT"])
           
        #Selenium script
        DRIVER_PATH = 'C:/Users/python/chromedriver.exe'
        driver = webdriver.Chrome(executable_path=DRIVER_PATH)
        driver.get('https://seti.afip.gob.ar/padron-puc-constancia-internet/ConsultaConstanciaAction.do')

        driver.implicitly_wait(20)
        cuit = driver.find_element_by_xpath("//input[@id='cuit']").send_keys(cuit_r)
        submit = driver.find_element_by_xpath("//input[@id='btnConsultar']").click()
            
        return render(request, 'app/constancia-inscripcion.html')

該網站也有一個驗證碼,我得到了處理它的方法,我是否也應該將驗證碼字段添加到 driver.find_element 並在那里傳遞該方法?

也許您的隱式等待方法還不夠。 您應該嘗試顯式等待。 基本上,顯式等待是您定義的代碼,用於等待特定條件發生,然后再繼續執行代碼: Source

另一個原因可能是動態 ID。 也許您的網站每次都會創建不同的 ID。 您應該找到其他每次都不會更改的屬性,然后根據此屬性更改您的 Xpath,或者您可以使用不需要任何特定屬性名稱的完整(絕對)Xpath。

暫無
暫無

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

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