0

Iam working on the robotframework, i want to highlight the element on the page below is the code i am getting the error

"AttributeError: 'str' object has no attribute '_parent' "

Below is the code,

import time

def highlight(element):
    """Highlights (blinks) a Selenium Webdriver element"""
    driver = element._parent
    def apply_style(s):
        driver.execute_script("arguments[0].setAttribute('style', arguments[1]);",
                              element, s)
    original_style = element.get_attribute('style')
    apply_style("background: yellow; border: 2px solid red;")
    time.sleep(.3)
    apply_style(original_style)

Common.robot

*** Settings ***
Library     SeleniumLibrary   15.0    5.0
Library    OperatingSystem
Library           highlight.py
Resource    ../LoginPage/LoginKW.robot

*** Variables ***
${URL}           https://www.involve.me/

*** Keywords ***
Start
    ${options}=  Evaluate        sys.modules['selenium.webdriver'].ChromeOptions()  sys, selenium.webdriver
    Log To Console                                 ${options}
    Create WebDriver    Chrome      chrome_options=${options}
    Go To          ${URL}
    ${handles}=  Get Window Handles
    Maximize Browser Window
    Verify Current URL
    ${SCREEN_TEXTS}  Read Json    ../../TestData/Involve_Test.json
    Set Global Variable    ${SCREEN_TEXTS}

Read Json
    [Documentation]    The function, with its' own keyword reads the json files.
    [Arguments]        ${file_path}
    ${JSONContent}     Get File     ${file_path}
    ${page}=           Evaluate        json.loads("""${JSONContent}""")    json
    [Return]           ${page}

Close My Browser
    Sleep  2s
    Close All Browsers

Click For Element
    [Arguments]                         ${element}
    Highlight                           ${element}
    Wait Until Element Is Visible       ${element}
    Click Element                       ${element}

Input For Text
    [Arguments]                         ${element}      ${input}
    Wait Until Element Is Visible       ${element}
    Highlight                           ${element}
    Input Text                          ${element}      ${input}

LoginTC.robot

*** Settings ***
Documentation       Test Case Login
Library             SeleniumLibrary
Resource            ../../PageObjects/LoginPage/LoginKW.robot
Resource            ../../PageObjects/Common/common.robot
Test Setup        Start  # Suitw Setup will run only once
Test Teardown      common.Close My Browser

*** Test Cases ***
Test Login
    sleep    3s
    Click Cookie
    Click On Link Login
    Enter Email                          [email protected]
    Enter Password                       abcdefghijk3Only
    Click On Sign in button
    Is Text Displayed     ${SCREEN_TEXTS["ws_page_title"]}

When i run the script getting the error.

(venv) C:\Users\User\PycharmProjects\InvolvemeRobotFramework\TestCases\Login> robot LoginTC1.robot

LoginTC1 :: Test Case Login

Test Login <selenium.webdriver.chrome.options.Options object at 0x041902B0>

DevTools listening on ws://127.0.0.1:61649/devtools/browser/4914f52f-47f6-4632-a776-02045dda890c https://www.involve.me/ ..F.....[28328:8120:0508/220557.776:ERROR:device_event_log_impl.cc(222)] [22:05:57.777] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connec tion: A device attached to the system is not functioning. (0x1F) Test Login | FAIL | AttributeError: 'str' object has no attribute '_parent'

Can someone please help me

4
  • What are you passing to highlight keyword? Because RF complains about str, I would guess you are passing XPath or similar as a string. You can debug it by logging the type and contents of the element variable. You can also try TRACE loglevel with -L TRACE. Commented May 10, 2023 at 6:27
  • yes, it's printing the xpath value, 16:31:52.241 INFO Value::: xpath://div[@id='cookiescript_accept'] 16:31:52.243 FAIL AttributeError: 'str' object has no attribute '_parent' to make this work it has to print like this <selenium.webdriver.remote.webelement.WebElement (session="98f5eab03abb5d613ecc7d12fd2849f3", element="a36621db-6a34-4ba0-9c82-45f68904d436")> how to print that in robot framework ? Commented May 10, 2023 at 20:35
  • 1
    Thank you for the info. It looks like the keyword Click Cookie fails. Somewhere in that keyword, you might have this keyword: Highlight //div[@id='cookiescript_accept']. You cannot pass XPath to highlight function because it requires an element. You need to have something like this before: ${element}= Get WebElement //div[@id='cookiescript_accept'] and pass ${element} to the Highlight keyword. Commented May 15, 2023 at 7:11
  • Thank you very much @Pekka your solution worked for me, yes I need to pass web element, Commented May 15, 2023 at 15:19

1 Answer 1

0

Change 'element' to 'self' because your class lack a constructor

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.