首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >转到网站按下关注按钮时出现错误

转到网站按下关注按钮时出现错误
EN

Stack Overflow用户
提问于 2019-01-30 22:27:21
回答 1查看 67关注 0票数 0

我正在尝试创建一个抽动追随者机器人,并已创建简单的代码去网站,并按下跟随按钮,但它没有点击跟随按钮。

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 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搜索。

下面是一个这样做的例子:

代码语言:javascript
复制
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).

  • attribute :属性的element.

  • selecction的div :其文本在目标attribute.

  • xpath :中的字符串可用于搜索子包装器元素。

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54442833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档