I am new to Python Selenium. I am stuck with this. Please help me in finding a solution.
I am trying to click this MENU1 using ActionChain.
locator = (By.XPATH, "//div[#title='MENU1']")
text_element = WebDriverWait(driver, 20).until(visibility_of_element_located(locator))
actions = ActionChains(driver)
actions.move_to_element(text_element).click().perform()
Below is the HTML:
<div _ngcontent-c0="" class="hyd-group-tree-node-label active" tooltipposition="bottom" title="MENU1">
<span _ngcontent-c0="" class="ui-treenode-inner-icon fa fa-fw fa-building"></span>
MENU1
</div>
Everything runs fine. No errors.
It moves to the element but I am not able to see the click movement.
I am not sure what I am missing. I tried with text_element.click() but it is not working. I tried in both Chrome and Firefox.
Thank you
The desired element is a Angular element so invoke click() 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(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.hyd-group-tree-node-label.active[title='MENU1']>span.ui-treenode-inner-icon.fa.fa-fw.fa-building"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='hyd-group-tree-node-label active' and #title='MENU1']/span[#class='ui-treenode-inner-icon fa fa-fw fa-building']"))).click()
If you still want to use ActionChains you can use either of the following solutions:
Using CSS_SELECTOR:
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.hyd-group-tree-node-label.active[title='MENU1']>span.ui-treenode-inner-icon.fa.fa-fw.fa-building")))).click().perform()
Using XPATH:
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='hyd-group-tree-node-label active' and #title='MENU1']/span[#class='ui-treenode-inner-icon fa fa-fw fa-building']")))).click().perform()
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
Related
I am trying to use selenium click to open dropdown menu and then click again to download a file. Common error I get is that the "element is not attached to the page document" Have tried different codes but here's one:
#button = driver.find_element_by_xpath('//ul[#class="dropdown-menu-right dropdown-menu"]')
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, "options-dropdown")))
element.click()
HTML snapshot:
Generally <ul> nodes along with it's decendent <li> nodes gets expanded clicking on <span> nodes.
Solution
To click and open the drop-down-menu i.e. to expand the <li> nodes you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.dropdown-toggle#options-dropdown"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='dropdown-toggle' and #id='options-dropdown']"))).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
I am trying to create a web application that automately republishes on a site for private sales.
Login and everything went well, but if I try to find and click the republish button it just wont work.
I've tried it with every locator, but the problem is that every button got an uniqe ID.
Example:
<button name="republish" type="button" data-testid="5484xxxxx-republish-button" class="Button__ButtonContainer-sc-3uxxxx-0 hxXxxX">Republish</button>
The last I've tried:
buttons = driver.find_elements(By.XPATH, "//*[contains(text(), 'Republish')]")
for btn in buttons:
btn.click()
But it also didnt work, same with By.NAME, BY.TAG_NAME
The <button> element looks to be dynamically generated so to click elements you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-testid$='republish-button'][name='republish']"))))
Using XPATH:
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[starts-with(#class, 'Button__ButtonContainer') and contains(#data-testid, 'republish-button')][#name='republish' and text()='Republish']"))))
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
I'm making some test with selenium on this page:
https://www.justwatch.com/es/proveedor/hbo-max/peliculas
The first time you get in the page you must accept the privacy settings but I cannot get it to work. I've tried all the methods I now but it seems like I can't find the button I want so the program stops as the waiting time ends. The button I want to click is:
<button role="button" data-testid="uc-save-button" class="sc-gsDKAQ eaUldE" style="margin: 0px 6px;">Guardar configuración</button>
I tried to locate the button by text, class and using the XPath but doesnt work.
WebDriverWait(driver, 5)\
.until(EC.element_to_be_clickable((By.CLASS_NAME, 'sc-gsDKAQ eaUldE'.replace(' ', '.'))))\
.click()
WebDriverWait(driver, 5)\
.until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]//div/div/div/div/div[2]/div/div[2]/div/div/div/button[1]')))\
.click()
WebDriverWait(driver, 5)\
.until(EC.element_to_be_clickable((By.LINK_TEXT, 'Guardar configuración')))\
.click()
I will appreciate your help.
To click on the desired element you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-testid='uc-save-button']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#data-testid='uc-save-button' and text()='Guardar configuración']"))).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
im trying to code a steambot in python that will post in steamgroup;Im having problem with last step,
i cant find the sumbit button
<button type="submit" class="btn_green_white_innerfade btn_medium" id="commentthread_General_34191408_submit">
<span>Post Discussion</span>
</button>
To identify the submit button with text as Post Discussion you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
element = driver.find_element_by_css_selector("button.btn_green_white_innerfade.btn_medium[id^='commentthread_General_'][id$='_submit']>span")
Using XPATH:
element = driver.find_element_by_xpath("//button[#class='btn_green_white_innerfade btn_medium' and starts-with(#id, 'commentthread_General_')]/span[text()='Post Discussion']")
However, as it is a submit button so moving ahead you will invoke click() on it, so you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn_green_white_innerfade.btn_medium[id^='commentthread_General_'][id$='_submit']>span"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn_green_white_innerfade btn_medium' and starts-with(#id, 'commentthread_General_')]/span[text()='Post Discussion']"))).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
Here is your button, works 100%. I checked it locally.
driver.find_element(By.XPATH, "//button[#type='submit']")
I am using selenium and chrome to automate the clicks of a page with python.
I am getting stuck not being able to click on the following href link:
<a href="javascript:void(0)" class="addSuppData-trigger pts" data-target="edit_3-1" style="padding-right:6px;float:right;">
<i class="material-icons black-text tiny-small">edit</i></a>
I have tried using xpath, css, and linktext to no avail.
sample code:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="row-group-1"]/td[1]/div/div[2]/a/i' ))).click()
The goal is to click the pen, then select the item from the drop down.
screen shot of button
The second highlight line is the pen.
html tree
The element seems to be a dynamic element and to click on it 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.addSuppData-trigger.pts[data-target^='edit_']>i.material-icons.black-text.tiny-small"))).click()
Using XPATH:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='addSuppData-trigger pts' and starts-with(#data-target, 'edit_')]/i[#class='material-icons black-text tiny-small' and contains(., 'edit')]"))).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