Skip to content

Commit

Permalink
Reduce timeout sleep time.
Browse files Browse the repository at this point in the history
  • Loading branch information
zw963 committed Apr 19, 2024
1 parent eb4f734 commit b7ea000
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/translater/baidu.cr
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Translater
# 百度有检测,不可以粘贴,必须全部手动输入
content.each_char do |e|
input_ele.send_keys(key: e.to_s)
sleep 0.05
sleep 0.01
end

if debug_mode
Expand Down
16 changes: 9 additions & 7 deletions src/translater/selenium/session.cr
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require "timeout"

class Selenium::Session
# def find_by_selector_timeout!(selector : String, *, was_hidden : Bool = false, timeout seconds : Number = 0.5)
# def find_by_selector_timeout!(selector : String, *, was_hidden : Bool = false, timeout seconds : Number = 0.2)
# element : Selenium::Element? = nil

# timeout(seconds) do
# until element = find_by_selector(selector, was_hidden: was_hidden)
# sleep 0.1
# sleep 0.05
# end
# end

Expand All @@ -17,27 +17,29 @@ class Selenium::Session
# exit(1)
# end

def find_by_selector_timeout(selector : String, *, was_hidden : Bool = false, timeout seconds : Number = 0.5)
def find_by_selector_timeout(selector : String, *, was_hidden : Bool = false, timeout seconds : Number = 0.2)
element : Selenium::Element? = nil

timeout(seconds) do
until (element = find_by_selector selector, was_hidden: was_hidden)
sleep 0.1
sleep 0.05
end
end

element.not_nil!
rescue e : Timeout::Error
# e.inspect_with_backtrace(STDERR)
STDERR.puts "CSS selector #{selector} timeout for #{seconds} seconds!"

nil
end

def find_by_selector_wait!(selector : String, *, was_hidden : Bool = false, &) : Selenium::Element
element = nil

loop do
until (element = find_by_selector selector, was_hidden: was_hidden)
sleep 0.1
sleep 0.05
end

result = yield element
Expand All @@ -53,7 +55,7 @@ class Selenium::Session

def find_by_selector_wait!(selector : String, *, was_hidden : Bool = false) : Selenium::Element
until (element = find_by_selector selector, was_hidden: was_hidden)
sleep 0.1
sleep 0.05
end

element
Expand All @@ -64,7 +66,7 @@ class Selenium::Session

def find_by_selector_wait_disappear!(selector : String, *, was_hidden : Bool = false)
while find_by_selector(selector, was_hidden: was_hidden)
sleep 0.1
sleep 0.05
end
rescue
STDERR.puts "CSS selector #{selector} is never disppear."
Expand Down
2 changes: 1 addition & 1 deletion src/translater/youdao.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Translater

session.navigate_to("https://fanyi.youdao.com/index.html#")

if (ele1 = session.find_by_selector_timeout ".pop-up-comp.mask img.close", timeout: 0.5)
if (ele1 = session.find_by_selector_timeout ".pop-up-comp.mask img.close")
ele1.click
end

Expand Down

0 comments on commit b7ea000

Please sign in to comment.