【问题标题】:Absolute Path: Selenium Xpath Can't Identify Element绝对路径:Selenium Xpath 无法识别元素
【发布时间】:2020-08-16 07:00:58
【问题描述】:

我正在尝试使用 Xpath 识别 HTML 按钮,并且尝试了相对和绝对 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()

【问题讨论】:

    标签: python html selenium automation


    【解决方案1】:

    您可以使用以下任何 Xpath: 更正继续按钮的相对 XPath

    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)]

    【讨论】:

    • 嗨 - 谢谢你的想法。我将 .click() 添加到两者的末尾,但都不起作用。你知道为什么吗?
    • @quantionaire 你能分享一下你得到了什么错误或输出吗?
    • click = webdriver.find.element_by_xpath('//button[@class="spr-btn spr-btn-primary" and contains(text(),"Continue")]').click () AttributeError: 'WebDriver' 对象没有属性 'find'
    • 有错别字
    【解决方案2】:

    试一试:

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

    【讨论】:

    • 感谢您的输入 - 这无法点击按钮。
    • 一定是其他原因导致失败,或许在此之前采取行动?更多带有堆栈跟踪错误的代码会很有用。
    • 我可以调用类似的 web.driver 获取元素函数,这些函数在此调用之前单击几乎相同的按钮。在这一行上,它正在生成错误:click= webdriver.find.element_by_xpath("//button[@class='spr-btn spr-btn-primary' and contains(text(),'Continue')]")。 click() AttributeError: 'WebDriver' 对象没有属性 'find'
    • 有一个错字...查看更新。这是webdriver.find_element_by_xpath...
    猜你喜欢
    • 2020-06-20
    • 2018-05-25
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多