What is the best way to check URL change with Selenium in Python?
I was thinking to do that somehow with WebDriverWait
Exactly. First of all, see if the built-in Expected Conditions may solve that:
title_is
title_contains
Sample usage:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
wait.until(EC.title_is("title"))
wait.until(EC.title_contains("part of title"))
If not, you can always create a custom Expected Condition to wait for url to match a desired regular expression.
Detecting URL change using Selenium WebDriver (JavaScript)
There are wait and until modules for this
driver.wait(until.urlIs('expected url'));
Selenium webdriver url changes automatically for unknown reason
I am the one who wrote the original post and found I found the solution to this problem. As Max Daroshchanka mentioned in his answer, the problem was claused by indeed.com as it reloaded due to some plugin (or something). Therefore my solution was to use the input field only after some time passed (using time.sleep(2))
Web driver doesn't wait for url change (python)
I don't know if I can accept this as an answer to this question, but this is what I found.
So, I was asked in comments to provide link to the website I'm using this on (which I couldn't do). After that, it occurred to me that it might be the website that is the problem, so I tested it on another website, and it works. Unfortunately, I don't know what is the issue with the website, but I'll leave this so someone who has the same problem sees that this can happen also (feel free to remove this if it shouldn't be labeled as an answer).
How to replace the url in a loop using Selenium Python
Use the range()
function and use String interpolation as follows:
for i in range(1,5):
print(f"https://test.com/page/{i}")
driver.get(f"https://test.com/page/{i}")
driver.find_element_by_xpath("/html/body/div/div/div[2]/form/section[3]/input[4]").click()
Console Output:
https://test.com/page/1
https://test.com/page/2
https://test.com/page/3
https://test.com/page/4
Parsing a site where URL doesn't change with Selenium Python
You need to make sure when you reach the next page, the content of the earlier page has become stale otherwise, you will have stale element error or get the same thing repeatedly. Try the below approach, it should get you there. The rest you can modify yourself.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get("http://www.incometaxindia.gov.in/Pages/utilities/exempted-institutions.aspx")
while True:
for elem in wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,"[id^='arrowex']"))):
print(elem.text)
try:
wait.until(EC.presence_of_element_located((By.ID, "ctl00_SPWebPartManager1_g_d6877ff2_42a8_4804_8802_6d49230dae8a_ctl00_imgbtnNext"))).click()
wait.until(EC.staleness_of(elem))
except:
break
driver.quit()
Related Topics
Check If File Has a CSV Format With Python
Getting S3 Objects' Last Modified Datetimes With Boto
How to Downgrade Tensorflow, Multiple Versions Possible
Make Alternate Letters Capital
How to Extract All Upper from a String - Python
Calculate Rgb Value for a Range of Values to Create Heat Map
Python: Filenotfounderror: [Winerror 3] the System Cannot Find the Path Specified: ''
Opencv - Saving Images to a Particular Folder of Choice
Check If List Is Ascending or Descending (Using For)
Pandas: Difference Between Pivot and Pivot_Table. Why Is Only Pivot_Table Working
Pyspark - Pass List as Parameter to Udf
Selecting Specific Rows of CSV Based on a Column'S Value in Python
Valueerror: Invalid \Escape Unable to Load Json from File
Sklearn Error Valueerror: Input Contains Nan, Infinity or a Value Too Large for Dtype('Float64')
Python: String Iteration Replace a Space With a Hyphen (Or Other Character)
Django Viewset Has Not Attribute 'Get_Extra_Actions'
Flask API Typeerror: Object of Type 'Response' Is Not Json Serializable