Coder Social home page Coder Social logo

Get element by XPath about arsenic HOT 2 CLOSED

hennge avatar hennge commented on May 12, 2024
Get element by XPath

from arsenic.

Comments (2)

ojii avatar ojii commented on May 12, 2024 1

No, though you could easily implement it yourself. see https://github.com/HDE/arsenic/blob/4eaafc8f62d6fb0305e66fbb63dcac12b0f35484/src/arsenic/session.py#L98-L104 for how the css-selector based get element works.

from arsenic.

philyoun avatar philyoun commented on May 12, 2024

Just to elaborate what @ojii said, below is what I did to use xpath.
Open site-packages\arsenic\session.py, in class Element(RequestHelpers):, add

    async def get_element_by_xpath(self, selector: str) -> "Element":
        element_id = await self._request(
            url="/element",
            method="POST",
            data={"using": "xpath", "value": selector}, # not "XPath" nor "xpath selector"
        )
        return self.session.create_element(element_id)

and in class Session(RequestHelpers):, add

    async def get_element_by_xpath(self, selector: str) -> "Element":
        element_id = await self._request(
            url="/element",
            method="POST",
            data={"using": "xpath", "value": selector}, # not "XPath" nor "xpath selector"
        )
        return self.create_element(element_id) # not self.session.create_element()

By doing this, I was able to use equivalent to driver.find_element_by_xpath() in Selenium.
Additionally, I needed to use wait_for_element with the xpath, so that I added

    async def wait_for_xpath(self, timeout: int, selector: str) -> Element:
        return await self.wait(
            timeout, partial(self.get_element_by_xpath, selector), NoSuchElement
        )

arsenic library was useful for doing asynchronic webdriver, but lacks a little bit of convenience maybe due to its less usage...
Hope it helps anyone.

from arsenic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.