点击使用 selenium python 但没有响应
clicking using selenium python but no response
I chose the xpath from here
This is the button I am trying to click
add =driver.find_element_by_xpath('//*[@id="root"]/div/main/div/section[4]/section/section[2]/section[2]/div[2]/div[1]/div/div/div[2]/div/div[2]/div')
add.click()
print("hey")
这是代码,add.click() 不会产生任何错误并且打印出来但按钮没有被点击..
最初添加按钮不在视口中,但当代码执行时它自动出现在视口中但没有任何反应。
我已经尝试了所有的操作,比如滚动到元素,并将元素放入 viewpoet 中,但仍然没有任何反应..
这是我得到 xpath 的地方
<div class="sc-1usozeh-8 kTTqJP">
<span class="sc-1usozeh-6 fTsfFl">Add</span>
<i class="rbbb40-1 MxLSp sc-1usozeh-4 TZpZK" size="14" color="#ED5A6B">.
<svg xmlns="http://www.w3.org/2000/svg" fill="#ED5A6B" width="14"
height="14" viewBox="0 0 20 20" aria-labelledby="icon-svg-title-
icon-svg-desc-" role="img" class="rbbb40-0 hoSSCx">
<title>plus</title>
<path d="M15.5 9.42h-4.5v-4.5c0-0.56-0.44-1-1-1s-1 0.44-1 1v4.5h-
4.5c-0.56 0-1 0.44-1 1s0.44 1 1 1h4.5v4.5c0 0.54 0.44 1 1 1s1-0.46
1-1v-4.5h4.5c0.56 0 1-0.46 1-1s-0.44-1-1-1z"></path>
</svg>
</i>
</div>
该元素是动态元素,所以点击该元素需要诱导WebDriverWait for the element_to_be_clickable()
and you can use either of the following :
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
更新
作为最后的手段,您可以使用 execute_script()
,如下所示:
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add']"))))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add']/.."))).click()
如果上述方法不起作用,请尝试
button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add']")))
driver.execute_script("arguments[0].scrollIntoView();", button)
action = ActionChains(driver)
action.move_to_element(button).click().perform()
您需要导入操作 class:
from selenium.webdriver.common.action_chains import ActionChains
I chose the xpath from here
This is the button I am trying to click
add =driver.find_element_by_xpath('//*[@id="root"]/div/main/div/section[4]/section/section[2]/section[2]/div[2]/div[1]/div/div/div[2]/div/div[2]/div')
add.click()
print("hey")
这是代码,add.click() 不会产生任何错误并且打印出来但按钮没有被点击..
最初添加按钮不在视口中,但当代码执行时它自动出现在视口中但没有任何反应。
我已经尝试了所有的操作,比如滚动到元素,并将元素放入 viewpoet 中,但仍然没有任何反应..
这是我得到 xpath 的地方
<div class="sc-1usozeh-8 kTTqJP">
<span class="sc-1usozeh-6 fTsfFl">Add</span>
<i class="rbbb40-1 MxLSp sc-1usozeh-4 TZpZK" size="14" color="#ED5A6B">.
<svg xmlns="http://www.w3.org/2000/svg" fill="#ED5A6B" width="14"
height="14" viewBox="0 0 20 20" aria-labelledby="icon-svg-title-
icon-svg-desc-" role="img" class="rbbb40-0 hoSSCx">
<title>plus</title>
<path d="M15.5 9.42h-4.5v-4.5c0-0.56-0.44-1-1-1s-1 0.44-1 1v4.5h-
4.5c-0.56 0-1 0.44-1 1s0.44 1 1 1h4.5v4.5c0 0.54 0.44 1 1 1s1-0.46
1-1v-4.5h4.5c0.56 0 1-0.46 1-1s-0.44-1-1-1z"></path>
</svg>
</i>
</div>
该元素是动态元素,所以点击该元素需要诱导WebDriverWait for the element_to_be_clickable()
and you can use either of the following
使用
XPATH
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
更新
作为最后的手段,您可以使用 execute_script()
,如下所示:
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add']"))))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add']/.."))).click()
如果上述方法不起作用,请尝试
button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add']")))
driver.execute_script("arguments[0].scrollIntoView();", button)
action = ActionChains(driver)
action.move_to_element(button).click().perform()
您需要导入操作 class:
from selenium.webdriver.common.action_chains import ActionChains