AttributeError:“str”对象没有“find\u element”属性

2024-09-30 06:25:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我是用python学习selenium的新手

问题陈述:-

尝试在chrome浏览器中标识对象时出现以下错误

AttributeError:'str'对象没有属性“find\u element”

我知道,当我从seleniumConfig.py类调用类变量“driver”时,类变量被声明为字符串,分配的web元素没有被引用,但不确定如何纠正这个基本问题。任何帮助都将不胜感激

注意:浏览器已启动,URL已正确应用,但当我要单击页面上的某个对象时,问题开始出现

机器人框架:-

*** Settings ***

Resource        /foo/boo/selenium_Keywords.robot

SampletestRun
    [Documentation]  To validate
    [Tags]  samplerun
    Launching Browser
    Login

selenium\u关键词.机器人

   *** Settings ***
    Library       /foo/boo/SeleniumConfig.py
    Library       /foo/boo/Pratice.py

    *** Keywords ***

    Launching Browser
        browser
        url 

   Login
        click_button    

SeleniumConfig.Py

from selenium.webdriver.chrome.options import Options
from selenium import webdriver

class SeleniumConfig():
  driver=""

def __init__(self):
  self.driverPath="foo\boo\chrome.exe"

def browser():
  SeleniumConfig.driver = webdriver.Chrome(self.driverPath)

def url():
 SeleniumConfig.driver.get("www.google.com")

def get_driver()
 return SeleniumConfig.driver

Selenium.py

from foo.boo.SeleniumConfig import SeleniumConfig
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains

class Selenium():
  driver=SeleniumConfig()

def __init__(self):
  self.driver = Selenium.driver.get_driver()

def click_object(webelement):
  element = WebDriverWait(self.driver, 10).until(
                    EC.visibility_of_element_located((By.ID, webelement)))
  ActionChains(self.driver).move_to_element(element).click(element).perform()

练习.py

from foo.boo.Selenium import Selenium

class Practice():
  sm=Selenium()

  def click_button(self):
        Practice.sm.click_object("webElement")

回溯结果:-

SampletestRun :: To validate                                          | FAIL |
AttributeError: 'str' object has no attribute 'find_element'

Tags: frompyimportselffoodefdriverselenium

热门问题