【问题标题】:How to send Xpath text through webhook?如何通过 webhook 发送 Xpath 文本?
【发布时间】:2021-08-08 12:30:00
【问题描述】:

所以我试图通过一个带有 selenium (python) 的不和谐 webhook 在这个 website 中发送这个 value。我怎么做?这是我到目前为止编写的代码:from selenium import webdriver

from discord_webhook import DiscordWebhook
from selenium.webdriver.common import service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service


#Import service=service
service=Service("./chromedriver")

#Chrome options :)
chrome_options = Options()
chrome_options.add_argument("no-sandbox")
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("user-data-dir=./user-data")

#Running chrome
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.get('https://www.coindesk.com/price/dogecoin')
price = driver.find_element_by_xpath('//*[@id="export-chart-element"]/div/section/div[2]/div/div/div/div/div').click()
price = driver.find_element_by_xpath('//*[@id="export-chart-element"]/div/section/div[2]/div/div/div/ul/li[3]').click()
price_send = driver.find_element_by_xpath('//*[@id="export-chart-element"]/div/section/div[1]/div[1]/div[2]/div').click()
#driver.quit()

#Send it through discord webhook
webhook = DiscordWebhook(url='https://discord.com/api/webhooks/844456788626505738/s53WWXxYdBRtjLsAw95zT99vcYJf4-Z7dGopRcCInywi3TCrSs11KYvgCEGQ84xhello', content='Price: '.format(price_send))
response = webhook.execute()
 

【问题讨论】:

    标签: python selenium discord webhooks


    【解决方案1】:

    首先点击位于//*[@id="export-chart-element"]/div/section/div[1]/div[1]/div[2]/div XPath 的元素什么都不做,因为这不是一个按钮。
    如果您尝试获取此元素文本,您应该执行类似的操作

    price_send = driver.find_element_by_xpath('//*[@id="export-chart-element"]/div/section/div[1]/div[1]/div[2]/div').text
    

    通常,您使用的选择器非常错误。
    例如代替
    '//*[@id="export-chart-element"]/div/section/div[1]/div[1]/div[2]/div'
    XPath 你应该简单地使用以下类名 price-large 或 css_selector .price-large 所以它会很简单

    price_send = driver.find_element_by_css_selector('.price-large').text
    

    【讨论】:

    • 之后我该如何发送?
    • 这个我真的不知道,对不起。我对 webhook 不熟悉。不过我想我的回答至少会部分帮助你。
    【解决方案2】:

    不是这个

    price_send = driver.find_element_by_xpath('//*[@id="export-chart-element"]/div/section/div[1]/div[1]/div[2]/div').click()
    

    这样做

    price_send = driver.find_element_by_xpath('//*[@id="export-chart-element"]/div/section/div[1]/div[1]/div[2]/div').text
    

    那个 number 是一个 webelement,你可以点击它以及提取文本。因此,在您的用例中,您可以先获取文本,然后根据您的意愿将其发送到 webhook 或任何其他文件。

    建议:

    使用相对路径总是比使用绝对路径更好,所以尝试像这样编写定位器

    //div[text()='Price']/../following-sibling::div/descendant::span
    

    【讨论】:

    • 我已经在 7 分钟前写过这篇文章了……而且还不止于此
    猜你喜欢
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 2021-10-18
    • 1970-01-01
    相关资源
    最近更新 更多