'Setting a default for nosuchelementexception for multiple variables in python
So I am scrapping multiple rows of a table and many of them are either available or not for different pages. What I want to do is to detect which field is not available and supply it on a variable and set a default variable i.e., None to it. For eg,
try:
field1 = driver.find_element(By.XPATH, value="somexpath")
field2 = driver.find_element(By.XPATH, value="somexpath")
field3 = driver.find_element(By.XPATH, value="somexpath")
field4 = driver.find_element(By.XPATH, value="somexpath")
field5 = driver.find_element(By.XPATH, value="somexpath")
dict_ = {"field1":field1, "field2": field2.....}
except NoSuchElementException:
# some code to detect which element not found and supply a default value None to it
defaultVaule = None
Please help.
Solution 1:[1]
You can inject if else None
statement as follows:
field1 = driver.find_element(By.XPATH, value="somexpath")
field1=field1.text if field1 else None
Solution 2:[2]
You may want to reverse the order - initialize using default values and populate if retrieval succeeds element-wise.
dict_ = {"field1":None, "field2": None.....}
try:
dict_["field1"] = driver.find_element(By.XPATH, value="somexpath")
try:
dict_["field2"] = driver.find_element(By.XPATH, value="somexpath")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | F.Hoque |
Solution 2 | firluk |