Search code examples
pythonselenium-webdriverweb-scraping

Problem in Find Element after page changed in Selenium Python


im using a web scraper library as [Selenium] in python. and i want to submit a form (multiple form without AJAX)

so , i wrote this code :

from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get('https://exportgram.com/')

one = driver.find_element_by_xpath('//*[@id="home"]/form/div[1]/div/input[1]')
one.click()
one.send_keys('https://www.instagram.com/p/B6OCVnTglgz/')
two = driver.find_element_by_xpath('//*[@id="home"]/form/div[2]/button')
two.click()
time.sleep(3)


three = driver.find_element_by_xpath('//*[@id="home"]/form/div[2]/button')
three.click()

now , i cant access "three" because thats available on new page.

isnt any way to resolve that problem?

[Note : after click on 'two' , page changed to 'https://example.com/media.php']


Solution

  • If after click on second element it redirect you to another page it means that this form is partially sent. You will be able to find a new DOM tree as soon as the browser loads the new address, so you have to wait for it to show up.

    You can do this by this code:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    timeout = 10
    three = WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.XPATH, '//*[@id="home"]/form/div[2]/button')), 'Time out')
    three.click()