1

I am a novice in learning selenium with python.

Problem Statement:-

Getting the following error while trying to identify an object in the chrome browser.

AttributeError: 'str' object has no attribute 'find_element'

I understand that the class variable is declared as a string and the assigned web element is getting not referred when I invoke class variable 'driver' from seleniumConfig.py class but not sure how to rectify this fundamental problem. Any help would be highly appreciated.

Note: The browser is getting launched and the URL is getting applied properly but the problem starts when I want to click an object on the page.

Robot Framework:-

*** Settings ***

Resource        /foo/boo/selenium_Keywords.robot

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

selenium_Keywords.robot

   *** 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()

Practice.py

from foo.boo.Selenium import Selenium

class Practice():
  sm=Selenium()

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

Traceback Results:-

SampletestRun :: To validate                                          | FAIL |
AttributeError: 'str' object has no attribute 'find_element'
5
  • In click_object(), does element actually store an element? Commented Apr 14, 2020 at 5:09
  • @Sri It is to click the given web element only. Actual issue is the call 'Selenium.driver.get_driver()' product empty string. Therefore script producing such error. Commented Apr 14, 2020 at 5:35
  • You dont need to give path of chrome.exe instead of that please download latest chrome driver which support to your browser version and provide binary path chromedriver.chromium.org/downloads Commented Apr 14, 2020 at 7:34
  • @DipakBachhav Thanks!. Any assistance on actual issue? Commented Apr 14, 2020 at 13:16
  • Any assistance? Commented Apr 14, 2020 at 15:54

1 Answer 1

1

Seems, Selenium on Python got confused after they marked as deprecated some find_elements with css locator.

element = WebDriverWait(self.driver, 10).until(
    EC.visibility_of_element_located((By.ID, webelement)))

changed to another method visibility_of and it works now: instead of tuple of By and locator it takes an element.

element = WebDriverWait(self.driver, 10).until(
    EC.visibility_of(browser.find_element(By.ID, webelement))
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.