Trying to find submit button in elements - python

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']")

Related

Finding a button with selenium in python

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

How to click on an element by title with Selenium Python

I'm trying to check a checkbox with selenium and the only available info is the title.
Is a dynamic element, so I need to select by the title.
I tried all the methods without success (contains #title, etc).
Can someone please help? I'm beginning to code.
The xpath is:
/html/body/div[7]/div[2]/div/div[2]/div/div[2]/div/div/div[1]/form/div/span[1]/input
The CSS Selector is:
#selectCommon > span:nth-child(1) > input:nth-child(1)
The HTML looks like:
<input type="checkbox" title="AM - AMERICAS">
To click() on the element with title as AM - AMERICAS you can use either of the following locator strategies:
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "input[title^='AM'][title$='AMERICAS']").click()
Using xpath:
driver.find_element(By.XPATH, "//input[starts-with(#title, 'AM') and contains(#title, 'AMERICAS')]").click()
Ideally to click() on the clickable element 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, "input[title^='AM'][title$='AMERICAS']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[starts-with(#title, 'AM') and contains(#title, 'AMERICAS')]"))).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

Clicking a button which is inside another element Selenium

HTML snapshot:
Hi I want press the button labelled log in on this page, the html for the button is as follows:
<button class="">Log In</button>
the problem is there is multiple log in buttons on the same page with this code so I want to specify this button by using the id of the container the button is in:
<li id = 'bs-bk-PP" class = "_2t6uLu"
How do I find and click the log in button using these properties.
Thanks
To click on the element with text as Log In you can use either of the following Locator Strategies:
Using xpath:
driver.find_element(By.XPATH, "//li[#id='bs-bk-PP']//button[#class and text()='Log In']").click()
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, "//li[#id='bs-bk-PP']//button[#class and text()='Log In']"))).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

How to click an element using Selenium Python

So I have this element in HTML...
<span style="font-size:11pt">Security</span>
... and I want to click it using Selenium in python. How can I do it? (Maybe I can identify it using onclick, but how?)
Also, here is what I've tried:
security = driver.find_element_by_xpath("//button[#onclick=\"on_catolog(2);\"]")
security.click()
You used wrong tag name in your XPath "//button[#onclick=\"on_catolog(2);\"]". It's not a button, but anchor tag. Try
"//a[#onclick='on_catolog(2);']"
Or click by link text:
security = driver.find_element_by_link_text("Security")
security.click()
To click on the element with text as Security you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("a[onclick^='on_catolog'] > span[style='font-size:11pt']").click()
Using xpath:
driver.find_element_by_xpath("//a[starts-with(#onclick, 'on_catolog')]/span[text()='Security']").click()
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 CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick^='on_catolog'] > span[style='font-size:11pt']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(#onclick, 'on_catolog')]/span[text()='Security']"))).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

How to click on an element through Selenium ActionChain and Python

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

Categories

Resources