如何使用Selenium和Python单击https://www.deepl.com/translator中的“复制”按钮

问题描述 投票:1回答:3

我正在尝试使用此URL https://www.deepl.com/translator自动完成硒的翻译。但是,我无法单击此处照片中显示的复制按钮。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 locator)来定位此按钮。我有义务。

我用来尝试找到按钮的代码是:

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 <button> is not clickable at point (1177,601) because another element <p> obscures it

python selenium xpath css-selectors webdriverwait
3个回答
1
投票

所需的元素是动态元素,因此单击该元素必须为WebDriverWait引入element_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
    
  • 浏览器快照:

deepl


0
投票

当我执行该链接并在执行任何操作之前将鼠标悬停在该链接上时,鼠标看起来好像不可点击。在我填写文本并翻译之后,它似乎可以单击,因为我的鼠标获得的是手指而不是箭头。

因此,我将尝试以下操作:首先翻译某些内容,然后尝试单击右侧的/翻译部分,然后尝试找到该按钮并单击它。

有时对我也有用的是复制完整的xpath而不是较短的xpath(chrome中有一个选项可以复制完整的xpath或简单地复制xpath)。

如果还是不行,我建议您看一下this question,因为有一些示例可能会出错,以及如何在答案中解决它。


0
投票

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

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()

Whith 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()
© www.soinside.com 2019 - 2024. All rights reserved.