Find all text inputs on a Haskell Web Package Package web page

Here is my attempt to find all text type inputs on a web page. Since then, I realized that I can use xpath, but I would like to know how to make it the way I tried to work. What interests me most is how I would pull up my [Element] in [WD Element] and make this program valid.

However, if my approach is simply wrong or uniomatic, feel free to completely rewrite it. Here is the code:

{-# LANGUAGE OverloadedStrings #-}

import           Control.Monad
import           Control.Monad.IO.Class
import           Test.WebDriver
import           Test.WebDriver.Classes       (WebDriver (..))
import           Test.WebDriver.Commands
import           Test.WebDriver.Commands.Wait

main = do
  runSession defaultSession capabilities $ do
      openPage "http://www.appnitro.com/demo/view.php?id=1"
      inputs <- findElems $ ByTag "input"
      textElems <- filterM (liftM $ ((==) "text" . (`attr` "type"))) inputs
      -- wait 20 seconds
      waitUntil 20 (getText <=< findElem $ ByCSS ".doesnotexist")
        `onTimeout` return ""
  liftIO $ putStrLn "done"
    where
      capabilities = allCaps { browser=firefox }

-- [1 of 1] Compiling Main             ( src/Main.hs, interpreted )

-- src/Main.hs:168:70:
--     Couldn't match type `Element' with `WD Element'
--     Expected type: [WD Element]
--       Actual type: [Element]
--     In the second argument of `filterM', namely `inputs'
--     In a stmt of a 'do' block:
--       textElems <- filterM
--                      (liftM $ ((==) "text" . (`attr` "type"))) inputs
--     In the second argument of `($)', namely
--       `do { openPage "http://www.appnitro.com/demo/view.php?id=1";
--             inputs <- findElems $ ByTag "input";
--             textElems <- filterM
--                            (liftM $ ((==) "text" . (`attr` "type"))) inputs;
--             waitUntil 20 (getText <=< findElem $ ByCSS ".doesnotexist")
--             `onTimeout` return "" }'
-- Failed, modules loaded: none.
+4
source share
3 answers

Replace text filtering with this part:

      textElems <- filterM textElem inputs

And then add this where:

      textElem e = (== Just "text") `fmap` (e `attr` "type")

waitUntil, . , .

+2

, , , haskell. XPath, :

textElems <- findElems $ ByXPath "//input[@type='text']"

CSS, , , ( - , ..)

textElems <- findElems $ ByCSS "input[type='text']"
+3

:

attr Maybe, Just "text".

, attr WD, ( , , attr):

textElems <- filterM (liftM ((==) (Just "text")) . (`attr` "type")) inputs

Or, on the contrary, since WDFunctor is also (I actually find it more understandable, you apply a pure function inside the monad returned attr):

textElems <- filterM (fmap ((==) (Just "text")) . (`attr` "type")) inputs
+2
source

Source: https://habr.com/ru/post/1547710/


All Articles