gpt4 book ai didi

haskell - 使用 Haskell webdriver 包查找网页上的所有文本输入

转载 作者:行者123 更新时间:2023-12-04 23:50:03 25 4
gpt4 key购买 nike

这是我在网页上查找“文本”类型的所有输入的尝试。从那以后,我发现我可以使用 xpath,但我想知道如何按照我尝试的方式工作。我最感兴趣的是如何将我的 [Element] 提升到 [WD Element] 中并使该程序有效。

但是,如果我的方法是错误的或单调的,请随时完全重写它。这是代码:

{-# 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.

最佳答案

这可能不是您正在寻找的答案,但我发现最好直接在定位器中对元素表达这些类型的约束,以免弄乱 haskell 源。正如您提到的,使用 XPath 是一种可能性:

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

但我通常更喜欢 CSS 选择器,它往往更简洁(可惜功能不那么强大——例如,你不能从给定元素遍历到它的父元素等)并且在这些简单的情况下也能正常工作
textElems <- findElems $ ByCSS "input[type='text']"

关于haskell - 使用 Haskell webdriver 包查找网页上的所有文本输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24650813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com