김우재
김우재

Reputation: 19

Wait elements to be visible after click and print elements that I need Selenium Python

I want to wait for the element to be visible after click and print the element that I need. But it just doesn't wait unless I use time.sleep. I want to use

WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'tbody')))

for efficiency.

Please help.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException


driver = webdriver.Chrome(executable_path=r"C:\Users\Kim woo jae\PycharmProjects\100개 키워드\chromedriver.exe")

ky = '화장대','침대', '고기'
for k in ky:
    driver.get("http://whereispost.com/seller/")
    box = driver.find_element_by_xpath('//*[@id="keyword"]')
    box.clear()
    box.send_keys(k)
    box.submit()
    wait = WebDriverWait(driver, 20)
    b = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'tbody')))
    print(b.text)

This is the code that I want to use. But it doesn't wait for the element to be visible.

driver = webdriver.Chrome(executable_path=r"C:\Users\Kim woo jae\PycharmProjects\100개 키워드\chromedriver.exe")

ky = '화장대','침대', '고기'

for k in ky:
    driver.get("http://whereispost.com/seller/")

    box = driver.find_element_by_xpath('//*[@id="keyword"]')
    box.clear()
    box.send_keys(k)
    box.submit()
  **time.sleep(3)**
    wait = WebDriverWait(driver, 20)
    b = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'tbody')))
    print(b.text)

If I add time.sleep(3), it works fine. But I don't want to wait for 3 seconds if elements are already visible.

Upvotes: 1

Views: 151

Answers (1)

0buz
0buz

Reputation: 3503

tbody is always there; it is empty until you do a search.

Try waiting for some results in tbody, such as:

wait = WebDriverWait(driver, 20)
b = wait.until(EC.visibility_of_element_located((By.XPATH, '//tbody//tr')))

Upvotes: 1

Related Questions