Python 正在查找使用Selenium、text和class属性的元素,但找不到元素?

Python 正在查找使用Selenium、text和class属性的元素,但找不到元素?,python,selenium,Python,Selenium,我使用这段代码来获取元素的属性,并找出以后定位它的最佳方法。输出显示文本和类都包含属性,但当试图查找时,程序失败。我在find_元素_by语句中使用了错误的语法吗 temp_name = active_element.get_attribute('name') temp_title = active_element.get_attribute('title') temp_text = active_element.get_attribute('text') temp_class = active

我使用这段代码来获取元素的属性,并找出以后定位它的最佳方法。输出显示文本和类都包含属性,但当试图查找时,程序失败。我在find_元素_by语句中使用了错误的语法吗

temp_name = active_element.get_attribute('name')
temp_title = active_element.get_attribute('title')
temp_text = active_element.get_attribute('text')
temp_class = active_element.get_attribute('class')

print('name is ' + str(temp_name))
try:
    name_elements = driver.find_elements_by_name(temp_name)
    if len(name_elements) > 0:
        print(str(len(name_elements)) + ' elements found using name: ')
        for element in name_elements:
            print(element)
    else:
        name_element = driver.find_element_by_name(temp_name)
        print(name_element)
except NoSuchElementException:
    print('Could not locate element using name')

print('title is ' + str(temp_title))
try:
    title_elements = driver.find_elements_by_xpath("//*[@title='" + str(temp_title) + "']")
    if len(title_elements) > 0:
        print(str(len(title_elements)) + ' elements found using title: ')
        for element in title_elements:
        print(element)
    else:
        title_element = driver.find_element_by_xpath("//*[@title='" + str(temp_title) + "']")
        print(title_element)
except NoSuchElementException:
    print('Could not locate element using title')

print('text is ' + str(temp_text))
try:
    text_elements = driver.find_elements_by_xpath("//tag[contains(text(), " + str(temp_text) + ")]")
    if len(text_elements) > 0:
    print(str(len(text_elements)) + ' elements found using text: ')
    for element in text_elements:
        print(element)
    else:
        text_element = driver.find_element_by_xpath("//tag[contains(text(), " + str(temp_text) + ")]")
        print(text_element)
except NoSuchElementException:
    print('Could not locate element using text')

print('class is ' + str(temp_class))
try:
    class_elements = driver.find_elements_by_class_name(temp_class)
    if len(class_elements) > 0:
        print(str(len(class_elements)) + ' elements found using class: ')
        for element in class_elements:
            print(element)
    else:
        class_element = driver.find_element_by_class_name(temp_class)
        print(class_element)
except NoSuchElementException:
    print('Could not locate element using class')

active_element = driver.switch_to.active_element
GetAttributes()
这是我得到的输出

name is 
Could not locate element using name
title is 
Could not locate element using title
text is Buy now on the CAH Store
Could not locate element using text
class is btn btn-gold
Could not locate element using class

您可以共享url或html吗?