首页 > 解决方案 > 绝对路径:Selenium Xpath 无法识别元素

问题描述

我正在尝试使用 Xpath 识别 HTML Button,并尝试了相对和绝对 Xpath,但均未成功。我正在尝试单击该按钮。

相对路径: click = webdriver.find.element_by_xpath("//onboarding-mobile-fixed-bottom-container/div[1]/div/sprout-button/button").click()

绝对路径:/html/body/cfapp-root/main/cfapp-spa-host/main/onboarding-root/div/div[1]/main/onboarding-business-phone/section/form/onboarding-next-button/onboarding-mobile-fixed-bottom-container/div[2]/sprout-button/button

absolute = webdriver.find.element_by_xpath("/html/body/cfapp-root/main/cfapp-spa-host/main/onboarding-root/div/div[1]/main/onboarding-business-phone/section/form/onboarding-next-button/onboarding-mobile-fixed-bottom-container/div[2]/sprout-button/button").click()

即使使用绝对 xpath(我知道,练习时皱眉头),我也无法单击按钮。

作为参考,我正在自动化:网站:https://account.kabbage.com/onboarding/data/number-of-employees;用户名:testingoverflow@aol.com;密码:Kabbage123

(点击完成申请;完成申请;在继续框上工作)

任何帮助深表感谢!!

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep, strftime
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import csv 
import xlrd

info = "info.xlsx"
openwb = xlrd.open_workbook(info)
inputws = openwb.sheet_by_index(0)

print(inputws.nrows)
print(inputws.ncols)

print(inputws.cell_value(1,0))

email_log = inputws.cell_value(2,0)
businesslog = inputws.cell_value(2,1)
firstname = inputws.cell_value(2,2)
lastname = inputws.cell_value(2,3)
phone = int(inputws.cell_value(2,4))
employees = int(inputws.cell_value(2,5))
business_types = inputws.cell_value(2,6)


print(email_log)
print(businesslog)
print(firstname)
print(lastname)
print(phone)

sleep(1)

chromedriver_path = 'C:/Users/Documents/Scale/Programs/chromedriver.exe' 
webdriver = webdriver.Chrome(executable_path=chromedriver_path)
webdriver.get('https://app.kabbage.com/signup/create_account')

sleep(1)
#input email
input_emails = webdriver.find_element_by_xpath('//*[@id="CreateAccount.EmailAddress_inner"]').send_keys(email_log)
sleep(1)
#re-input email
reinput = webdriver.find_element_by_xpath('//*[@id="CreateAccount.ConfirmEmail_inner"]').send_keys(email_log)

# Password
passwrd  = webdriver.find_element_by_xpath('//*[@id="CreateAccount.CreatePassword"]')
sleep(1)
passwrd.send_keys('Paycheck11!!')
sleep(1)
button_started = webdriver.find_element_by_class_name("btn-full-width").click()

sleep(5)

#ApplyNow

#apply = webdriver.find_element_by_class_name('spr-btn spr-btn-primary')

#apply = webdriver.find_elements_by_class_name("spr-btn-primary").click()

#xpath("//div[@class='fc-day-content' and text()='15']")

applynow = webdriver.find_element_by_xpath("//sprout-button/button[contains(@class, 'spr-btn-primary')]").click()

sleep(5)

applyfinal = webdriver.find_element_by_xpath("//sprout-button/button[contains(@class, 'spr-btn-primary')]").click()

sleep(5)

business_name = webdriver.find_element_by_xpath('//*[@id="businessName-input"]').send_keys(businesslog)
business_send = webdriver.find_element_by_xpath("/html/body/cfapp-root/main/cfapp-spa-host/main/onboarding-root/div/div[1]/main/onboarding-business-name/section/form/onboarding-next-button/onboarding-mobile-fixed-bottom-container/div[2]/sprout-button/button").click()

sleep(5)

first_name = webdriver.find_element_by_xpath('//*[@id="lastName-input"]').send_keys(lastname)
last_name = webdriver.find_element_by_xpath('//*[@id="firstName-input"]').send_keys(firstname)
names_send = webdriver.find_element_by_xpath("/html/body/cfapp-root/main/cfapp-spa-host/main/onboarding-root/div/div[1]/main/onboarding-personal-name/section/form/onboarding-next-button/onboarding-mobile-fixed-bottom-container/div[2]/sprout-button/button").click()

sleep(5)

phone_num = webdriver.find_element_by_xpath('//*[@id="businessPhone-input"]').send_keys(phone)
phone_check = webdriver.find_element_by_xpath('//html/body/cfapp-root/main/cfapp-spa-host/main/onboarding-root/div/div[1]/main/onboarding-business-phone/section/form/kbg-consent-box/div/sprout-checkbox/div/label').click()
#phone_send = names_send = webdriver.find_element_by_xpath("/html/body/cfapp-root/main/cfapp-spa-host/main/onboarding-root/div/div[1]/main/onboarding-personal-name/section/form/onboarding-next-button/onboarding-mobile-fixed-bottom-container/div[2]/sprout-button/button").click()
phone_submits = webdriver.find_element_by_xpath("/html/body/cfapp-root/main/cfapp-spa-host/main/onboarding-root/div/div[1]/main/onboarding-business-phone/section/form/onboarding-next-button/onboarding-mobile-fixed-bottom-container/div[2]/sprout-button/button").click()

sleep(5)

num_empl = webdriver.find_element_by_xpath('//*[@id="numberOfEmployees-input"]').send_keys(employees)
#emp_submit = webdriver.find_element_by_xpath("//sprout-button/button[contains(@class, 'spr-btn-block')][2]").click()
sending = webdriver.find.element_by_xpath("//button[@class='spr-btn spr-btn-primary' and contains(text(),'Continue')]").click()

标签: pythonhtmlseleniumautomation

解决方案


您可以使用这些 Xpath 中的任何一个: Correct relative XPath for Continue button

Xpath 1:

*//onboarding-next-button//onboarding-mobile-fixed-bottom-container//div[2]//sprout-button//button[contains(text(),Continue)]

Xpath 2:

*//sprout-button[@class='desktop-button']//button[contains(text(),Continue)]


推荐阅读