我正在尝试创建一个抽动追随者机器人,并已创建简单的代码去网站,并按下跟随按钮,但它没有点击跟随按钮。
import webbrowser
url = "https://twitch.tv/owlcrogs"
driver = webbrowser.open(url)
follow_button =
driver.find_element_by_xpath(get_page_element("follow_button"))
follow_button.click()发布于 2019-01-30 23:02:15
get_page_element("follow_button")在哪里定义?您必须确保它返回一个有效的xpath。
在Google Chrome中,您可以右键单击目标元素并选择inspect来检查xpath。然后部署开发人员工具。右键单击开发人员工具中的选定项,然后单击>> copy >> copy Xpath
例如,driver.find_element_by_xpath('//*@id="id_element"/div2/a/span').click()
如果没有,那一定是get_page_element出了问题。返回的错误类型是什么?
我刚刚检查了一下按钮,也许你应该把‘follow- webPage’用连字符代替'follow_button‘,用下划线代替。但是,我希望通过数据目标属性xD进行get_page_element搜索。
下面是一个这样做的例子:
def find_element_by_attribute(wrapper, attribute, selection=None, xpath = None):
if selection is not None:
element = list(filter(lambda x: x.get_attribute(attribute) is not None and \
x.get_attribute(attribute).find(selection) != -1,\
wrapper.find_elements_by_xpath('.//*' if xpath is None else xpath)))
else:
element = list(filter(lambda x: x.get_attribute(attribute) is not None,\
wrapper.find_elements_by_xpath('.//*' if xpath is None else xpath)))
return None if len(element) is 0 else element[0]目标子包装器:搜索目标元素的页面元素(例如,带有用于选择目标tags).
import webbrowser
from auxiliars import find_element_by_attribute
url = "https://twitch.tv/owlcrogs"
driver = webbrowser.open(url)
follow_button = find_element_by_attribute(driver, 'data-a-target', selection='follow-button')
follow_button.click()https://stackoverflow.com/questions/54442833
复制相似问题