How to get the changed tagname ?
zhangsanfu opened this issue · 3 comments
When I open The http://product.dangdang.com/20528119.html,the tagname is "加入购物车",5 seconds later,the tagename is changed to "不再销售",I write code below,But It return None,How to get the changed tagname ?Thank you
from requestium import Session, Keys
import time
s = Session(webdriver_path='chromedriver/chromedriver.exe',
browser='chrome',
default_timeout=15,
webdriver_options={'arguments': ['headless']})
scode = s.get('http://product.dangdang.com/20528119.html')
tagname = s.driver.ensure_element_by_xpath("//div[@class='buy_box_btn']",state = 'invisible',timeout = 5).ensure_element_by_class_name()
print(tagname)
This code won't work since ensure_element_by_xpath
returns a Selenium WebElement which has no ensure_element_by_class_name
method, that's a driver method.
You should also use s.driver.get('http://product.dangdang.com/20528119.html')
if you want Chrome to load the site. Otherwise, you would just get a response (using get
method from requests
module) but it would not be loaded by the driver.
Regarding your initial question, the site actually changes that text after a few seconds, but when the driver finishes loading the site, that tagname is already changed so it shouldn't be a problem.
This code prints 不再销售
s.driver.get('http://product.dangdang.com/20528119.html')
tagname = s.driver.ensure_element_by_xpath("//div[@class='buy_box_btn']/a", timeout=5).get_attribute('innerHTML')
print(tagname)
Hi @zhangsanfu ,
As @fabalbertoni remarked, you're trying to use s.driver.ensure_element_by_xpath
method without making the corresponding get using the s.driver
first.
s.driver.get('http://product.dangdang.com/20528119.html')
Also, please take into account that ensure_element_by_class_name
method takes, at least, one argument which indicates the class name of the element you want to get.
So, if you want to use this method, it should be:
session.driver.ensure_element_by_class_name('buy_box_btn',timeout=5).get_attribute('innerHTML')
This returns the element with buy_box_btn
class.
Thanks for all!@fabalbertoni @SoleRivas
In fact ,use the new methods added by Requestium.
I found the new methods here:
'ensure_element_by_class_name', 'ensure_element', 'ensure_element_by_xpath', 'selector', 'ensure_element_by_partial_link_text', 'get_network_conditions', 'create_options', 'set_network_conditions', 'default_timeout', 'ensure_element_by_link_text', 'css', 'is_cookie_in_driver', 'ensure_element_by_css_selector', 'ensure_element_by_id', 're_first', 're', 'ensure_element_by_tag_name', 'ensure_add_cookie', 'ensure_element_by_name', 'launch_app', 'xpath'
This is a cool module!