简体   繁体   English

Selium 无法定位类元素

[英]Selium Unable to Locate Class Element

Dear Stackoverflowers,亲爱的 Stackoverflowers,

I'm trying to automate a CC payment process but Selenium is having a hard time identifying a specific element I want to click on.我正在尝试自动执行 CC 付款流程,但 Selenium 很难确定我想要单击的特定元素。 I'm trying to click on 'REI Card - 6137' so that I can continue to the payment page.我正在尝试单击“REI 卡 - 6137”,以便我可以继续进入付款页面。 Using the inspect tool it shows the class as, "soloLink accountNamesize".使用检查工具,它将类显示为“soloLink accountNamesize”。 Unfortunately, there's not an ID I can go after.不幸的是,没有我可以追求的 ID。 When I try to search by class name I get this error in the console:当我尝试按类名搜索时,我在控制台中收到此错误:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .soloLink accountNamesize selenium.common.exceptions.NoSuchElementException:消息:无法定位元素:.soloLink accountNamesize

Below is a picture of the site and the inspector pane with the thing I'm trying to click on highlighted in blue.下面是站点和检查器窗格的图片,其中我尝试单击的内容以蓝色突出显示。 Since its my credit card and I'm already logged it a link to the page wouldn't really help you guys.因为它是我的信用卡并且我已经记录了它,所以指向该页面的链接不会真正帮助你们。

在此处输入图片说明

The script gets hung up on "driver.find_element_by_class_name('soloLink accountNamesize').click()"脚本挂在“driver.find_element_by_class_name('soloLink accountNamesize').click()”上

My code is below:我的代码如下:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import yaml
import time
conf = yaml.load(open(r'D:\Users\Matt\Documents\GitHub\YML_Files\REI_Login_Credentials.yml'))
myREIUsername = conf['REILogin']['username']
myREIPassword = conf['REILogin']['password']

driver = webdriver.Firefox(
    executable_path=
    r'D:\Users\Matt\Documents\GitHub\Executable_Files\geckodriver.exe'
)

def login():
   driver.get('https://onlinebanking.usbank.com/Auth/Login?usertype=REIMC&redirect=login&lang=en&exp=')
   time.sleep(4)
   driver.find_element_by_id('aw-personal-id').send_keys(myREIUsername)
   driver.find_element_by_id('aw-password').send_keys(myREIPassword)
   time.sleep(2)
   driver.find_element_by_id('aw-log-in').click()
   time.sleep(15)
   make_payment()

def make_payment():
    if (driver.find_element_by_class_name("accountRowLast").text) != "0.00":
        driver.find_element_by_class_name('soloLink accountNamesize').click()


    else:
        driver.quit()

I've tried searching by Xpath and Xpath + Class with no luck.我尝试通过 Xpath 和 Xpath + Class 进行搜索,但没有运气。 I also tried searching for this issue but its a fairly unique class so I didn't have much luck.我也尝试搜索这个问题,但它是一个相当独特的类,所以我没有太多运气。 Have any other ideas I could try?我还有其他想法可以尝试吗?

soloLink accountNamesize is multiple class names use the following css selector instead to click on that element. soloLink accountNamesize 是多个类名,请使用以下 css 选择器来单击该元素。

driver.find_element_by_css_selector('a.soloLink.accountNamesize').click()

To induce waits we do为了诱导等待,我们做

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.soloLink.accountNamesize"))).click()

Based on the photo, I think that this is the xpath that you might want根据照片,我认为这是您可能想要的xpath

//div[@id='MyAccountsDiv']//div[@id='CreditsTableDiv']//tbody//tr[@class='accountRowFirst']//a[contains(@onclick, 'OpenAccountDashboard')]

As you can see, this xpath starts off with the top-most div that might be unique ( MyAccountsDiv ) and continues to dive into the HTML code.如您所见,此xpath从可能是唯一的最顶层div ( MyAccountsDiv ) 开始,并继续深入HTML代码。

Based off of this, you could click on the link with the following code基于此,您可以单击带有以下代码的链接

xpath = "//div[@id='MyAccountsDiv']//div[@id='CreditsTableDiv']//tbody//tr[@class='accountRowFirst']//a[contains(@onclick, 'OpenAccountDashboard')]"
driver.find_element(By.XPATH, xpath).click()

NOTE笔记
Your error says你的错误说

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="aw-personal-id"]

Maybe you can use the above technique and see if you can isolate the xpath for the web element instead.也许您可以使用上述技术,看看是否可以为 web 元素隔离xpath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM