n00n
n00n

Reputation: 664

find_element by css not working as expected

new with selenium, I got stuck with some basics in Python.

I need to check if this src is set correctly

<div id="body_logo"><img src="#local"></img></div>

That's what I have tried (and many other tips from internet)

result = driver.find_element(By.CSS_SELECTOR("#body_logo img"))

Now, I would like to test if the image src contains a specific string.

I tried:

result = driver.find_element(By.CSS_SELECTOR("#body_logo"))

Which is given by several tutorials. Tried other ways, too. But I always failed.

local:~/Projects/selenium$ python3 loginTest.py 
[WDM] - 

[WDM] - ====== WebDriver manager ======
[WDM] - Current google-chrome version is 94.0.4606
[WDM] - Get LATEST driver version for 94.0.4606
[WDM] - Driver 

[/home/dev/.wdm/drivers/chromedriver/linux64/94.0.4606.61/chromedriver] found in cache
<selenium.webdriver.chrome.webdriver.WebDriver         
    (session="51db98ae3610912d240402aed0b8a1f0")>
    Traceback (most recent call last):
        File "loginTest.py", line 17, in <module>
        test = (driver.find_element(By.CSS_SELECTOR("#body_logo")))
        TypeError: 'str' object is not callable

Any hint for me?

Upvotes: 0

Views: 88

Answers (1)

cruisepandey
cruisepandey

Reputation: 29372

Instead of

driver.find_element(By.CSS_SELECTOR("#body_logo img"))

use this :

result = driver.find_element(By.CSS_SELECTOR, "#body_logo img")

You have syntax issue. Please remove ) and have ,

to check if a specific string is present or not.

Code :

result = driver.find_element(By.CSS_SELECTOR, "#body_logo img")
actual_src = result.get_attirbute('src')
if "string_to_search" in actual_src:
    print('specific string found')
else:
    print('specific string not found')

Upvotes: 1

Related Questions