简体   繁体   中英

Unable to click on a box in Selenium Webdriver using Python

I want to click on the box which has 24h as text while testing in Selenium Webdriver using Python but unable to do so. Python code:

from selenium import webdriver
from PIL import Image
from selenium.webdriver.chrome.service import Service
import time

service = Service('/Users/XYZ/Desktop/Selenium/chromedriver.exe')
service.start()
driver = webdriver.Remote(service.service_url)
driver.get('example.com')
driver.maximize_window()
driver.find_element_by_name("ctl00$BodyContent$Username").send_keys("id")
driver.find_element_by_name("ctl00$BodyContent$Password").send_keys("pwd")
driver.find_element_by_id("ctl00_BodyContent_LoginButton").click()

driver.execute_script("document.body.style.zoom='75%'")
driver.execute_script("window.scrollTo(0, 225);")

time.sleep(3)## lets say 3 seconds
driver.find_element_by_xpath("//*[@id='highcharts-114']/svg/g[19]/text").click()
driver.save_screenshot("screenshot.png")

HTML Code:

<g zIndex="7" states="[object Object]" style="cursor:default;text-align:center;" transform="translate(164,61)"><rect rx="0" ry="0" fill="url(#highcharts-114)" x="0.5" y="0.5" width="27" height="18" stroke-width="1" stroke="#cccccc"></rect><text x="2.1875" y="14" style="font-family:&quot;Lucida Grande&quot;, &quot;Lucida Sans Unicode&quot;, Verdana, Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold;color:#4d4d4d;fill:#4d4d4d;" zIndex="1"><tspan x="2.1875">24h</tspan></text></g>

<rect rx="0" ry="0" fill="url(#highcharts-114)" x="0.5" y="0.5" width="27" height="18" stroke-width="1" stroke="#cccccc"></rect>

<text x="2.1875" y="14" style="font-family:&quot;Lucida Grande&quot;, &quot;Lucida Sans Unicode&quot;, Verdana, Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold;color:#4d4d4d;fill:#4d4d4d;" zIndex="1">
<tspan x="2.1875">24h</tspan>
</text>

Error Message:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='highcharts-114']/svg/g[19]/text"} Box HTML code image

Please provide full code.A blind guess is that probably your page doesnt load in time so element doesnt exist.There are two ways: The bad:

time.sleep(3)## lets say 3 seconds
driver.find_element_by_xpath("//*[@id='highcharts-114']/svg/g[19]/text").click()

and the wait method provided by selenium(recomended).

wait = WebDriverWait(driver, 10)
inputBox = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='highcharts-114']//*[name()='svg']//*[name()='g'][19]//*[name()='text']")))
inputBox.click()

Note: please add below imports to your solution

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM