絵を描いたりネットいろいろ
スポンサーサイト
上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。
ruby win32ole IE操作  あと  rubyからwsh

なんかちょうどいいのあったので、
でも古かったから、ちょっと手直しして動くようにした

http://www.tech-notes.dyndns.org/win32ole/ie_lib_document.html



require 'win32ole'

class IE_Wrapper
def initialize(raw_object, ie_obj=nil)
@ie = ie_obj
@raw_object = raw_object
end


def raw
@raw_object
end

private

def method_missing(m_id, *params)
missing_method_name = m_id.to_s
# downcase method name
if methods.include?(m_id.to_s.downcase)
send(m_id.to_s.downcase.intern, *params)
else
begin
@raw_object.send(m_id, *params)
rescue
raise $!,$!.message, caller
end
end
end

def inspect()
self.class
end

def set_ie(ie_obj)
@ie = ie_obj
end

COMPLETE_STATE = 4
def wait_until_stable(ie)
return unless ie
while ie.busy == true
sleep 0.5
end

loop do
break if ie.readyState == COMPLETE_STATE
sleep 0.5
end
end


end

# ========================
# IE
# ========================
class IE < IE_Wrapper
def initialize(visible_flag = true)
@raw_object = WIN32OLE.new("InternetExplorer.Application")
@ie = @raw_object
if visible_flag
@ie.visible = true
end
end

def document()
doc = Document.new(@ie.document, @ie)
end

def navigate(url)
@ie.navigate(url)
wait_until_stable(@ie)
end

def wait_stable()
wait_until_stable(@ie)
end

# ========================
# IE.Document
# ========================
class Document < IE_Wrapper
def frames(index=nil)
if index
return(nil) if index >= @raw_object.frames.length
Frames.new(@raw_object.frames, @ie)[index]
else
Frames.new(@raw_object.frames, @ie)
end

end

def all()
TagElementCollection.new(@raw_object.all, @ie)
end

def tags(tag_name)
TagElementCollection.new(@raw_object.all.tags(tag_name), @ie)
end

def head()
TagElementCollection.new(@raw_object.all.tags("HEAD"), @ie)[0]
end

def body()
TagElementCollection.new(@raw_object.all.tags("BODY"), @ie)[0]
end
end


# ========================
# TAG Element Collection
# ========================
class TagElementCollection < IE_Wrapper
def [](index)
if index.class == Fixnum
return(nil) if index >= @raw_object.length
TagElement.new(@raw_object.item(index), @ie)
elsif index.class == Range
raise "Range Not Support for TagElementCollection#[]"
end
end

def size
@raw_object.length
end

def each
@raw_object.each {|tag_element|
yield TagElement.new(tag_element, @ie)
}
end

def get_tag_by_title(target_str)
get_tag_by_key(target_str, "VALUE")
end
def get_tag_by_value(target_str)
get_tag_by_key(target_str, "VALUE")
end

def get_tag_by_text(target_str)
get_tag_by_key(target_str, "INNERTEXT")
end

def get_tag_by_name(target_str)
get_tag_by_key(target_str, "NAME")
end

def get_tag_by_option(target_str)
get_tag_by_key(target_str, "INNERHTML")
end

def inspect()
element_inspect_list = []
self.each {|tag_element|
element_inspect_list.push tag_element.inspect
}
"#{self.class},#{element_inspect_list.join('/')}"
end

private

def get_tag_by_key(target_str, key_type)
tag_list = []
@raw_object.each {|tag_element|
case key_type
when "INNERTEXT"
key_string = tag_element.innerText.gsub(/\s/, "")
when "VALUE"
key_string = tag_element.value.gsub(/\s/, "")
when "NAME"
key_string = tag_element.name
when "INNERHTML"
key_string = tag_element.innerHTML.gsub(/\s/, "")
else
return nil
end
if key_string =~ /#{target_str}/
tag_list.push TagElement.new(tag_element, @ie)
end
}
case tag_list.size
when 0
return nil
when 1
return tag_list[0]
else
return tag_list
end
end
end

# ========================
# TAG Element
# ========================
class TagElement < IE_Wrapper
def tagname
@raw_object.tagName
end

def text=(set_text)
case tagName
when "SELECT"
option_list = tags("OPTION")
option_list.each {|option_element|
if option_element.innerText == set_text
option_element.selected = true
break
end
}
else
@raw_object.value = set_text
end
end

def inspect()
case tagName
when "SELECT"
innerHTML = replace_cr_code(self.innerHTML)
"#{self.class}:<#{tagName}>:[#{self.innerHTML}]"
when "INPUT", "IMG", "A"
outerHTML = replace_cr_code(self.outerHTML)
"#{self.class}:<#{tagName}>:[#{self.outerHTML}]"
when "TR", "TD"
innerText = replace_cr_code(self.innerText)
"#{self.class}:<#{tagName}>:[#{innerText}]"
else
"#{self.class}:<#{tagName}>"
end
end

def to_s
@raw_object.value
end

def click
@raw_object.click
wait_until_stable(@ie)
end

def tags(tag_name)
TagElementCollection.new(@raw_object.all.tags(tag_name), @ie)
end

def all
TagElementCollection.new(@raw_object.all, @ie)
end

private
def replace_cr_code(text)
replcae_text = text.gsub(/\r/, '\r')
replcae_text.gsub!(/\n/, '\n')
return replcae_text
end
end


# ========================
# IE.Document.Frames
# ========================
class Frames < IE_Wrapper
def [](index)
return(nil) if index >= @raw_object.length
Frame.new(@raw_object.item(index), @ie)
end

def size
@raw_object.length
end

def each
index = 0
while index < @raw_object.length
raw_frame = @raw_object.item(index)
ie_frame = Frame.new(raw_frame, @ie)
yield(ie_frame)
index += 1
end
end
end

# ========================
# IE.Document.Frames.item(n)
# ========================
class Frame < IE_Wrapper
def document
Document.new(@raw_object.document, @ie)
end
end
end




ie = IE.new false
# true だとIE可視化で起動
#ie.navigate("http://www.google.com/")
ie.navigate("http://hayabusa.2ch.net/test/read.cgi/news4vip/1328169422/l50")


input_list = ie.document.body.tags("input")
input_list.each {|element|
p element
}

input_list.get_tag_by_name('FROM').text = "歳納京子ちゃんぺろぺろ"
input_list.get_tag_by_name('mail').text = "sage"

input_list2 = ie.document.body.tags("textarea")
input_list2.each {|element|
p element
}
input_list2.get_tag_by_name('').text = "ぺろぺろ"

input_list.get_tag_by_title("書き込む").click



これはIEのクッキーそのまま使う感じなので
mechanizeとはまた違うね

2chへの書き込みも余裕になった

これ  p element
 で表示してるときは
IE::TagElement:こうなってるけど
input_list2.get_tag_by_name('MESSAGE').text = "ぺろぺろ"

でも、いけた
elemetn表示のほうがミスってるのかもね
FORMに複数のtextarea あるときは、タグのnameから名前引っ張ってきて指定




wsh
-------------------------------


require 'win32ole'
wsh = WIN32OLE.new('WScript.Shell')
wsh.SendKeys "{TAB}"
wsh.SendKeys " "
sleep 1


__END__

Set objShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 1000
objShell.SendKeys "{TAB}"

objShell.SendKeys " "



エンドレスバトルがレベル上がって飽きてきたので
さらに色々自動化をしちゃう
なにがたのしいの・・・


さいきん絵かいてない
ぁああぁぁぁあぁかかないとゆゆゆゆ
    0..

kakikomi
上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。