Home > other >  Is the element location obtained by Xpath with Python Selenium after the browser has rendered it?
Is the element location obtained by Xpath with Python Selenium after the browser has rendered it?

Time:10-09

The following pages,https://www.zjzwfw.gov.cn/zjservice/front/index/page.do?webId=1, I would like to locate the '城乡居民养老保险参保登记',However, this text information is not in the web source code,but I can get this element information correctly.I'm very curious about this. The code is as follows:


from selenium import webdriver
from selenium.webdriver.common.by import By
import chromedriver_autoinstaller
from selenium.webdriver.chrome.service import Service
from xpath_helper import xh, filter
chromedriver_autoinstaller.install('/Users/project/chromedriver')
options = webdriver.ChromeOptions()
options.add_argument('--headless')
browser = webdriver.Chrome(service=Service())
browser.get("https://www.zjzwfw.gov.cn/zjservice/front/index/page.do?webId=1")

#%%
el = xh.get_element(filter.value_contains(str('城乡居民养老保险参保登记')))
html1 = browser.find_element(By.XPATH,str(el))
html1.click() ## It runs correctly.

CodePudding user response:

Yes, you're right, the element is generated after rendering.

Some elements are JavaScript generated, as a result might not be readily present in the source until browser renders.

Occasionally, you might find out that the element isn't found and as a result you might have to wait until element is visible using selenium webdriverwait

CodePudding user response:

By applying browser.get() method Selenium will wait for the page to be loaded according to selected Page Load Strategy. The default Page Load Strategy is normal. According to the documentation in this page loading strategy WebDriver should wait for the document readiness state to be "complete" after navigation.
So, the answer to your question is Yes, in this specific case Selenium will wait for page to be loaded.
And this is not dependent what locator are you using XPath, CSS Selector, ID or any other.
Important to understand, that if Selenium command browser.find_element(By.XPATH,str(el)) were coming after some other command, like element.click() Selenium would not wait for that element rendered until you use WebDriverWait, like wait.until(EC.element_to_be_clickable((By.ID, "the_id"))).click()
This is because the default value of driver implicit wait is 0 (documentation).

  • Related