python - Try alternative xpaths, or else continue -
i have pieced web crawler selenium uses xpath find elements. on web page i'm scraping, there 2 possible layouts loaded, depending on content.
if run code on wrong layout, error: message: unable locate element: {"method":"xpath","selector":
how can create try
/except
(or similar) tries alternative xpath, if first xpath not present? or if none present, continue on next section of code?
i haven't got experience python i'm not able write example code, should create 2 try/catch
(or in case try/except
) block try find element find_element_by_xpath
. after catch nosuchelementexception , can work webelement(s).
in java looks this:
boolean isfirstelementexist, issecondelementexist = true; webelement firstelement, secondelement; try { firstelement = driver.findelement(by.xpath("first xpath")); } catch (nosuchelementexception e) { isfirstelementexist = false; } try { secondelement = driver.findelement(by.xpath("second xpath")); } catch (nosuchelementexception e) { issecondelementexist = false; } //... work webelement
Comments
Post a Comment