セレンでNoSuchElementExceptionを回避する方法は?[複製]
Aug 18 2020
このすべてに新しいので、それが愚かな質問であるならば、私は謝罪します。
これが私がやろうとしていることのすべてです:
nxt_btn = driver.find_element_by_xpath('/html/body/main/article/section/form/div[2]/button')
nxt_btn.click()
time.sleep(2)
vote_check = driver.find_element_by_xpath('/html/body/main/article/section/form/div[1]/div[2]/div/div/fieldset/div/div/div[2]/div/input')
vote_check.click()
time.sleep(2)
nxt_btn = driver.find_element_by_xpath('/html/body/main/article/section/form/div[2]/button')
nxt_btn.click()
driver.quit()
そしてそれは戻ります:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/main/article/section/form/div[1]/div[2]/div/div/fieldset/div/div/div[2]/div/input"}
(Session info: chrome=84.0.4147.125)
待機時間を変更し、さまざまな要素を選択して、iframe内にあるかどうかを確認しようとしました。ある場合は、パスを上るだけでは見つかりません。
回答
1 AmrutaPande Aug 18 2020 at 12:18
スリープの代わりにSeleniumの明示的な待機を使用します
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "XPATH"))
使用できる条件は次のとおりです。
- 可視性_of_element_located
- text_to_be_present_in_element
- element_to_be_clickable