Home > Back-end >  How to get this attribute value in python using selenium?
How to get this attribute value in python using selenium?

Time:10-13

I have a code that goes on instagram, connect to my account, enter passcode, and search a user. I want to see if aria-disabled is "true". I simply want to print if it's set to 'true'.

I just can't seem to make to make it work.

<div aria-disabled="true" role="button" tabindex="-1">

The code :

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

#specify the path to chromedriver.exe (download and save on your computer)
driver = webdriver.Chrome('C:/Users/me/Desktop/IG_APP/chromedriver.exe')

#open the webpage
driver.get("http://www.instagram.com")

#target username
username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']")))
password = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password']")))

#enter username and password
username.clear()
username.send_keys("MYUSERID")
password.clear()
password.send_keys("MYPASSWORD")

#target the login button and click it
button = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()

not_now = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Not Now")]'))).click()
not_now2 = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Not Now")]'))).click()

import time

# target the search input field
searchbox = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@placeholder='Search']")))
searchbox.clear()

# search for the user
keyword = "@test"
searchbox.send_keys(keyword)


time.sleep(2)
searchbox.send_keys(Keys.ENTER)
time.sleep(2)
searchbox.send_keys(Keys.ENTER)
time.sleep(2)

When I found my user, All I want is to know, if the attribute aria-disabled is "true"

CodePudding user response:

You can use 'get_attribute()' function.

Ex:

value = driver.find_element(By.XPATH,"<put the correct xpath here>").get_attribute("aria-disabled")
  • Related