I'm having an issue clicking an icon/link in a table. I've tried both find_element_by_xpath and find_elements_by_Xpath - no luck with either. I forced the wait as I was getting some issues with the element not being found.
I've highlighted the icon in the Table row, with the red box. in this image.
Website
Also found the Xpath but can't seem to get it to work, the icon is clickable on the webpage.
Xpath
My code is below:
driver.implicitly_wait(7)
tr = driver.find_element_by_xpath('//*[#id="AthleteTheme_wt6_block_wtMainContent_wt9_wtClassTable_ctl05_AthleteTheme_wt221_block_wtIconSvg_Svg"]/svg/use')
tr.click()
Thanks
The Element is in an svg tag. And there is a different syntax for the same. Links to refer - Link1, Link2
To access svg tag elements the syntax would something like this:
//*[local-name()='svg']
As per the screen shot the xpath for the Elements would be:
//span[#id="AthleteTheme_wt6_block_wtMainContent_wt9_wtClassTable_ctl05_AthleteTheme_wt221_block_wtIconSvg_Svg"]/*[local-name()='svg']/*[local-name()='use']
You can not directly use // to locate an SVG element. They are one of the special tags.
Always use //*[name()='svg'] or //*[local-name()='svg'] to locate them.
Based on the HTML that you've shared, Please use the below xpath :
//a[#class='svgContainer']//child::span//*[name()='svg' and starts-with(#id,'AthleteTheme')]//*[name()='use']
** Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
Code trial 1 :
time.sleep(5)
driver.find_element_by_xpath("//a[#class='svgContainer']//child::span//*[name()='svg' and starts-with(#id,'AthleteTheme')]//*[name()='use']").click()
Code trial 2 :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='svgContainer']//child::span//*[name()='svg' and starts-with(#id,'AthleteTheme')]//*[name()='use']"))).click()
Code trial 3 :
time.sleep(5)
button = driver.find_element_by_xpath("//a[#class='svgContainer']//child::span//*[name()='svg' and starts-with(#id,'AthleteTheme')]//*[name()='use']")
driver.execute_script("arguments[0].click();", button)
Code trial 4 :
time.sleep(5)
button = driver.find_element_by_xpath("//a[#class='svgContainer']//child::span//*[name()='svg' and starts-with(#id,'AthleteTheme')]//*[name()='use']")
ActionChains(driver).move_to_element(button).click().perform()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
Related
I'm trying to click buttons based on their text (using selenium). e.g., for the following HTML tag:
<span class="MuiTab-wrapper jss483">Options</span>
I've tried:
driver.find_elements_by_xpath("//*[contains(text(), 'Options')]").click()
However, Selenium can't find any element with the text: "Options".
Any ideas?
Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
xpath that you should check :
//*[contains(text(), 'Options')]
or
//span[contains(text(), 'Options')]
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
There are 4 ways to click in Selenium.
Code trial 1 :
time.sleep(5)
driver.find_element_by_xpath("//span[contains(text(), 'Options')]").click()
Code trial 2 :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(), 'Options')]"))).click()
Code trial 3 :
time.sleep(5)
button = driver.find_element_by_xpath("//span[contains(text(), 'Options')]")
driver.execute_script("arguments[0].click();", button)
Code trial 4 :
time.sleep(5)
button = driver.find_element_by_xpath("//span[contains(text(), 'Options')]")
ActionChains(driver).move_to_element(button).click().perform()
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
Also, find_elements returns a list, change that to find_element you should be good to go ahead.
or else use list index to point web element.
elems = driver.find_elements_by_xpath("//*[contains(text(), 'Options')]")
elems[0].click()
This is not recommended way.
You are using find_elements_by_xpath method. It will give you a list of web elements, not a single web element. So you can not apply .click() on the result of
driver.find_elements_by_xpath("//*[contains(text(), 'Options')]")
You should use find_element_by_xpath instead so your code will be
driver.find_element_by_xpath("//*[contains(text(), 'Options')]").click()
or clicking on the first result (or any other) from the web elements list returner, as following
driver.find_elements_by_xpath("//*[contains(text(), 'Options')]")[0].click()
Also possibly you need to add some delay / wait to let the element be loaded before accessing it.
Or maybe you need to scroll that element into the view.
Or maybe the element is inside an iframe...
To click on the element with text as Options you can use either of the following Locator Strategies:
Using xpath:
driver.find_element(By.XPATH, "//span[contains(#class, 'MuiTab-wrapper') and contains(., 'Options')]").click()
The desired element is a JavaScript enabled element, so ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(#class, 'MuiTab-wrapper') and contains(., 'Options')]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
This is the HTML of the page:
<td class="titulo_lateral" onclick="javascript: abreMenu("layer8");" style="cursor:pointer;">RELATÓRIOS</td>
I'm trying this:
driver.find_element_by_xpath("//*[#id='f']/table/tbody/tr/td/table[2]/tbody/tr/td")
Are you sure that the td, and thus the corresponding XPath, has been rendered on the page when that line of Selenium has executed?
If so, you can try using the full XPath rather than the relative XPath Copy Full XPath button pictured here
You can try with below xpath.
//td[#class='titulo_lateral']
or
//td[text()='RELATÓRIOS' and contains(#onclick,'abreMenu')]
but first you will have to make sure that if it is unique in HTMLDOM or not.
PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
once we have unique matching xpath. you can try to click on it by below methodology.
Code trial 1 :
time.sleep(5)
driver.find_element_by_xpath("//td[text()='RELATÓRIOS' and contains(#onclick,'abreMenu')]").click()
Code trial 2 :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[text()='RELATÓRIOS' and contains(#onclick,'abreMenu')]"))).click()
Code trial 3 :
time.sleep(5)
button = driver.find_element_by_xpath("//td[text()='RELATÓRIOS' and contains(#onclick,'abreMenu')]")
driver.execute_script("arguments[0].click();", button)
Code trial 4 :
time.sleep(5)
button = driver.find_element_by_xpath("//td[text()='RELATÓRIOS' and contains(#onclick,'abreMenu')]")
ActionChains(driver).move_to_element(button).click().perform()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
I am trying to use selenium to click on an element with x-onclick property not onclick.
I am using this element's xPath to click on it. These are the methods I have tried:
driver.execute_script("arguments[0].click();", element)
element.click()
but these don't work. I would love if someone can tell me a solution.
When you tried this
driver.execute_script("arguments[0].click();", element)
element is a web element. I do not know if you have defined it or not. If not defined then you must have got compile time error.
Anyway this looks to me an angular based application. So I would try with below code trials :
There are 4 ways to click in Selenium.
I will use this xpath
//a[#id='generater' and #x-onclick]
Code trial 1 :
time.sleep(5)
driver.find_element_by_xpath("//a[#id='generater' and #x-onclick]").click()
Code trial 2 :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#id='generater' and #x-onclick]"))).click()
Code trial 3 :
time.sleep(5)
button = driver.find_element_by_xpath("//a[#id='generater' and #x-onclick]")
driver.execute_script("arguments[0].click();", button)
Code trial 4 :
time.sleep(5)
button = driver.find_element_by_xpath("//a[#id='generater' and #x-onclick]")
ActionChains(driver).move_to_element(button).click().perform()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
Have you tried to click by creating xpath by using tag 'a' and 'id' by using simple selenium webdriver click method.
ele = driver.find_element_by_xpath("//a[#id='generater']")
ele.click()
or
driver.find_element_by_xpath("//a[#id='generater']").click()
if above xpath is unique then it should work else use javascript for clicking on above element 'ele'.
I want to locate (and click) the "Reoni" element, but I do not know what function to use it for
I tried with
driver.find_element_by_class_name("oe_menu_leaf")
and
driver.find_element_by_class_name("oe_menu_text")
but then selenium raise an error element cant be located,
and I tried
driver.find_element_by_link_text("Reoni")
This is the element I want to locate:
<a href="/web#menu_id=86&action=99" class="oe_menu_leaf" data-menu="86" data-action-model="ir.actions.act_window" data-action-id="99">
<span class="oe_menu_text">
Reoni
</span>
</a>
and full html:
If I was not clear enough or if you needed my code, please let me know.
As the desired element is a dynamic element you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.oe_menu_leaf[href*='/web#menu_id=']>span.oe_menu_text"))).click()
Using XPATH and text():
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='oe_menu_leaf' and starts-with(#href,'/web#menu_id=')]/span[#class='oe_menu_text' and text()='Reoni']"))).click()
Using XPATH and normalize-space():
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='oe_menu_leaf' and contains(#href,'/web#menu_id=')]/span[#class='oe_menu_text' and normalize-space()='Reoni']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Reference
You can find a relevant detailed discussion in:
Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
Try something like this:
Clicking the button
From Chrome :
Right click "inspect" on the item you are trying to find the xpath.
Right click on the highlighted area on the console.
Go to Copy xpath
selectElem=browser.find_element_by_xpath('x-path-here').click()
Reading Values Only
from bs4 import BeautifulSoup
innerHTML = browser.execute_script("return document.body.innerHTML")
soup = BeautifulSoup(str(innerHTML.encode('utf-8').strip()), 'lxml')
value = soup.find('span', attrs={'class':'fxst-calendarpro fxst-table-s1'}).text
Here is my python code
elements=driver.find_elements_by_id("folder_c0fb056b-f83a-495e-9db1-dd1b19942eaa")
print(elements)
the result is [ ]. The same way has worked for other parts of the website but when I get into a table inside the website, it does not work correctly. I could find and click some links prior to these command lines. Please look at the images I attached. When you look at HTML2, the white box is div "folder_c0fb056b-f83a-495e-9db1-dd1b19942eaa" shown in the last attached image (Webpage view). The rest blue boxes are the regions I am interested in. It looks like
<a id="rptReportFolders_ctl04_rptReports_ctl00_hlReportNameDescription" href="/da2/Reports/ReportResults.aspx?ReportID={b3583c5f-874b-4eca-9548-bf88f99ff7e6}">
<b>AE-CR Log - Open Items / All Projects</b>
</a>
If you are sure, that id of the elements is not generic you can try to use this:
elements = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//div[#id='folder_c0fb056b-f83a-495e-9db1-dd1b19942eaa']/div/a")))
for element in elements:
print(element.text)
if element.text == 'your text':
element.click()
The main idea of this code snippet is to use WebDriverWait to wait at least 10 seconds until all elements will be present on the page and only then print the list of them. Also I have proposed another selector which will locate all a links.
Note: you have to add some imports:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By