python – selenium webdriver wait until condition met

huangapple go评论57阅读模式
英文:

python - selenium webdriver wait until condition met

问题

这是用于在满足条件时点击Selenium WebDriver上的元素,并在条件不满足时刷新页面的代码。如果条件满足,它会点击"create_it"元素,如果条件不满足,它会刷新页面。但是需要注意的是,你使用了if wait_new:elif wait_old: 来检查条件,但这不是一个适当的方式来检查元素是否存在。你应该使用tryexcept 来处理元素查找异常,这样可以更好地处理元素是否存在的情况。如果需要进一步的协助,请告诉我。

英文:

i want to make the selenium webdriver to click something when the condition met, and to refresh the page if the condition not met, is this the right code for the condition met or not? i need enlightenment, because this code isn't executed properly

wait = ui.WebDriverWait(driver, 5)
wait_new = wait.until(lambda driver: driver.find_element(By.NAME, 'create_it'))
wait_old = wait.until(lambda driver: driver.find_element(By.NAME, 'submit_it'))
if wait_new:
    time.sleep(1)
    item = driver.find_element(By.NAME, 'create_it')
    item.click()
    time.sleep(2)
elif wait_old:
    time.sleep(1)
    driver.refresh()
    time.sleep(2)

答案1

得分: 2

你需要在一个try...except语句中执行等待操作,并捕获TimeoutException,以防查找元素失败。尝试使用内置的预期条件。

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

wait = WebDriverWait(driver, timeout=10)
try:
    elem = wait.until(EC.presence_of_element_located((By.NAME, 'create_it')))
    driver.execute_script("arguments[0].scrollIntoView({behavior: 'smooth', block: 'end', inline: 'end'});", elem)
    wait.until(EC.element_to_be_clickable(elem)).click()
except TimeoutException:
    driver.refresh()

对于submit_it元素,采用相同的处理方式。

英文:

You need to execute the wait in a try...except statement and catch the TimeoutException should the finding of the element fail. And try to use the built in expected conditions

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

wait = WebDriverWait(driver, timeout=10)
try:
    #elem = wait.until(lambda driver: driver.find_element(By.NAME, 'create_it'))
    elem = wait.until(EC.presence_of_element_located((By.NAME, 'create_it')))
    driver.execute_script("arguments[0].scrollIntoView({behavior: 'smooth', block: 'end', inline: 'end'});", elem)
    wait.until(EC.element_to_be_clickable(elem)).click()
except TimeoutException:
    driver.refresh()

Follow same process for the submit_it element

答案2

得分: 0

从您的代码中可以看出,您似乎在等待一个元素出现后再进行点击操作。对于这种用例,Selenium 提供了显式等待的功能,例如:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "create_it")))

这段代码将每隔500毫秒尝试定位元素,直到超时(10秒)。

英文:

From your code above, it seems that you're waiting for an element to be present before clicking it. For use cases like this, Selenium provides explicit waits, for instance:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "create_it")))

This piece of code will keep trying to locate the element every 500 ms, until it times out (10 seconds).

huangapple
  • 本文由 发表于 2023年4月17日 20:46:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定