Home > Enterprise >  find value of attribute with selenium
find value of attribute with selenium

Time:01-13

I have an object: enter image description here I need to get the content of item-id how do I do it?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import random as r
import time

driver = webdriver.Chrome(executable_path="C:\\Users\\user\\Documents\\לימודים\\python\\selenium-project\\Driver\\chromedriver.exe")

driver.get("https://www.google.com/")
driver.get("https://www.yad2.co.il/realestate/forsale?topArea=100&area=7&city=3000")

time.sleep(5)
# find all apartments
n = r.randint(1, 10)
print(n)

time.sleep(n)

listed_bulletin_clickable_elements = driver.find_elements(By.XPATH, '//*[@id="__layout"]/div/main/div/div[4]/div[5]/div[2]/div[5]/div[@]')
print(listed_bulletin_clickable_elements)

n = r.randint(1, 10)
print(n)
time.sleep(n)

# find the item-id
item-id = listed_bulletin_clickable_elements[2].get_attribute("item-id")
print(item-id)

CodePudding user response:

You will need some more imports:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time as t

[...]
wait = WebDriverWait(driver, 25)
[..]
products = wait.until(EC.presence_of_all_elements_located((By.XPATH, '//div[contains(@id, "feed_item_")]')))
for p in products:
    print(p.get_attribute('item-id')

See relevant Selenium documentation here.

  • Related