【问题标题】:How to click on the Copy button within https://www.deepl.com/translator using Selenium and Python如何使用 Selenium 和 Python 在 https://www.deepl.com/translator 中单击复制按钮
【发布时间】:2020-10-03 02:12:26
【问题描述】:

我正在尝试使用此 url https://www.deepl.com/translator 通过 selenium 自动进行一些翻译。但是,我无法单击此处照片中显示的复制按钮。red marking on button。检查这会显示此 html 代码

<button tabindex="130" _dl-connected="1" _dl-attr="onClick: $0.doCopy" _dl-attr-type="null">
                <svg width="20" height="22" viewBox="0 0 20 22" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M16.2949 15.7893H8.59364C7.36747 15.7893 6.37793 14.7947 6.37793 13.5623V3.22705C6.37793 1.9946 7.36747 1 8.59364 1H16.3164C17.5425 1 18.5321 1.9946 18.5321 3.22705V13.5839C18.5106 14.7947 17.521 15.7893 16.2949 15.7893Z" stroke-miterlimit="10"></path>
                    <path d="M11.1966 20.9997H3.34478C2.05408 20.9997 1 19.9402 1 18.6429V7.35629C1 6.05898 2.05408 4.99951 3.34478 4.99951H11.1966C12.4873 4.99951 13.5414 6.05898 13.5414 7.35629V18.6645C13.5414 19.9402 12.4873 20.9997 11.1966 20.9997Z" fill="white" stroke-miterlimit="10"></path>
                </svg>
            </button>

请指导如何使用 xpath(这里应该使用什么标签和属性)和另一种方法说 css 定位器来定位这个按钮。我有义务。

我用来定位按钮的代码是:

cpy_btn = driver.find_elements_by_xpath('//*[@id="dl_translator"]/div[1]/div[4]/div[3]/div[4]/div[1]/button')

后来我用了

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//button[@tabindex='130'])")))

但两者都不起作用。

我收到的错误信息是:selenium.common.exceptions.ElementClickInterceptedException: Message: Element &lt;button&gt; is not clickable at point (1177,601) because another element &lt;p&gt; obscures it

【问题讨论】:

  • 你能展示你试图做什么来点击它吗?我的简单答案是,通过查看页面代码来复制 xpath,但我假设你已经尝试过了。
  • @Jem 你是对的,我试过那个但没有用,这是用的 cpy_btn = driver.find_elements_by_xpath('//*[@id="dl_translator"]/div[1 ]/div[4]/div[3]/div[4]/div[1]/button') 之后我使用了 WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(/ /button[@tabindex='130'])"))) 但两者都没有。消息是 selenium.common.exceptions.ElementClickInterceptedException:消息:元素

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

您可以尝试添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

然后使用:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//textarea)[2]"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='lmt__target_toolbar__copy']/button"))).click()

使用 ActionChains :

from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='lmt__target_toolbar__copy']/button")))).click().perform()

【讨论】:

  • 很高兴你找到了一些有用的东西。我已经添加了 ActionChains 替代方案以防...
【解决方案2】:

想要的元素是动态元素,所以点击元素你必须诱导WebDriverWaitelement_to_be_clickable(),你可以使用以下Locator Strategies之一:

  • 使用CSS_SELECTOR

    driver.get('https://www.deepl.com/translator')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.lmt__target_toolbar__copy > button"))).click()
    
  • 使用XPATH

    driver.get('https://www.deepl.com/translator')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='lmt__target_toolbar__copy']/button"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 浏览器快照:

【讨论】:

  • 我也会尝试您的解决方案,但实际上我只是通过获取文本框的值而不是单击复制按钮来解决问题。 bttn=WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//textarea)[2]"))) translate_data=bttn.get_attribute('value')
  • xpath 不起作用,但 css 选择器起作用了,谢谢
猜你喜欢
  • 2020-12-31
  • 2021-04-09
  • 2018-08-22
  • 2019-09-01
  • 1970-01-01
  • 2016-12-31
  • 2021-04-07
  • 2019-09-16
  • 2018-04-30
相关资源
最近更新 更多