Decoding Class names on facebook through Selenium

后端 未结 1 955
悲哀的现实
悲哀的现实 2020-12-12 04:05

I noticed that facebook has some weird class names that look computer generated. What I don\'t know is if these classes are at least constant over time or they change in som

相关标签:
1条回答
  • 2020-12-12 04:15

    You were pretty correct. Facebook is built through ReactJS which is pretty much evident from the presence of the following keywords and tags within the HTML DOM:

    • {"react_render":true,"reflow":true}
    • <!-- react-mount-point-unstable -->
    • ["React-prod"]
    • ["ReactDOM-prod"]
    • ReactComposerTaggerType:{r:["t5r69"],be:1}

    So, the dynamically generated class names are bound to change after certain timegaps.


    Solution

    The solution would be to use the static attributes to construct a dynamic Locator Strategy.

    To retrieve the first line of the address just below the text FIND US you need to induce WebDriverWait in conjunction with expected_conditions as visibility_of_element_located() and you can use the following optimized solution:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[normalize-space()='FIND US']//following::span[2]"))))
    

    References

    You can find some relevant discussions in:

    • Logging Facebook using selenium
    • Why Selenium driver fail to recognize ID element of Facebook login page?

    Outro

    Note: Scrapping Facebook violates their Terms of Service of section 3.2.3 and you are liable to be questioned and may even land up in Facebook Jail. Use Facebook Graph API instead.

    0 讨论(0)
提交回复
热议问题