Fix wait time for Selenium Form using Python - python

I'm new to Python and am trying to automate a form fill up. I have done all the work but when trying to "Submit" it's not reading the XPATH and hence not clicking.
HTML:
<input style="text-align: center; height: 24px;" onclick="return (Button.OnClick(this, event));" onfocus="return (Button.OnFocus(this, event));" id="FormControl_V1_I1_B12" scriptclass="Button" class="t_cP1RLqWVMhs644yI_0 bg_cP1RLqWVMhs644yI_0 a4_cP1RLqWVMhs644yI_0" wrapped="true" direction="ltr" viewdatanode="13" formid="FormControl" originalid="V1_I1_B12" tabindex="0" title="" buttonid="CTRL10_5" value="Submit" type="button">
Here is the Python Code (the 2nd code handles the Ok popup):
click_value = web.find_element(By.XPATH('//*[#id="FormControl_V1_I1_B12"]')).get_attribute("onclick")
print(click_value)
web.switch_to.alert.accept()
Element snapshot:
Here is the HTML tag:

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, "input#FormControl_V1_I1_B12[formid='FormControl'][value='Submit']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='FormControl_V1_I1_B12' and #formid='FormControl'][#value='Submit']"))).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

Related

Fix Sign in error using Selenium in Python [duplicate]

I'm new to Python and am trying to automate a form fill up. I have done all the work but when trying to "Submit" it's not reading the XPATH and hence not clicking.
HTML:
<input style="text-align: center; height: 24px;" onclick="return (Button.OnClick(this, event));" onfocus="return (Button.OnFocus(this, event));" id="FormControl_V1_I1_B12" scriptclass="Button" class="t_cP1RLqWVMhs644yI_0 bg_cP1RLqWVMhs644yI_0 a4_cP1RLqWVMhs644yI_0" wrapped="true" direction="ltr" viewdatanode="13" formid="FormControl" originalid="V1_I1_B12" tabindex="0" title="" buttonid="CTRL10_5" value="Submit" type="button">
Here is the Python Code (the 2nd code handles the Ok popup):
click_value = web.find_element(By.XPATH('//*[#id="FormControl_V1_I1_B12"]')).get_attribute("onclick")
print(click_value)
web.switch_to.alert.accept()
Element snapshot:
Here is the HTML tag:
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, "input#FormControl_V1_I1_B12[formid='FormControl'][value='Submit']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='FormControl_V1_I1_B12' and #formid='FormControl'][#value='Submit']"))).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 find this element in selenium python

In the web page, there is many elements like this below and I want to find all of them and click one after one.
they have the same name of class but different by ID.
How I can find it and click?
<a class="do do-task btn btn-sm btn-primary btn-block" href="javascript:;" data-task-id="1687466" data-service-type="3" data-do-class="do-task" data-getcomment-href="/tasks/getcomment/" data-check-count="0" data-max-check-count="2" data-money-text="0.08"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">0.08 </font></font><i class="far fa-ruble-sign"></i></a>
To identify all of the similar elements and create a list you have to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
element_list = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a.do.do-task.btn.btn-sm.btn-primary.btn-block[data-task-id]")))
Using XPATH:
element_list = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//a[#class='do do-task btn btn-sm btn-primary btn-block' and #data-task-id]")))
Next, you can access each list item and invoke click() on each of them one by one.
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
If this css
a.do.do-task.btn.btn-sm.btn-primary.btn-block
represent them, then you can use the below code :
for a in driver.find_elements_by_css_selector("a.do.do-task.btn.btn-sm.btn-primary.btn-block"):
a.click()
You may have to scroll to each element or put some delay before click, this depends on your use case and your automation strategy.

How to locate tag-button (Angular Component) in Selenium?

Here is the HTML of the button I am trying to locate, it appears to me as an angular component:
<tab-button class="tab-button _ngcontent-jne-2 _nghost-jne-3" focusitem="" role="tab" aria-selected="false" tabindex="-1" aria-disabled="false" aria-label="Offline Reporting">
<!---->
<div class="content displayed-value _ngcontent-jne-3">Offline Reporting</div>
<!---->
<material-ripple class="_ngcontent-jne-3">
<div class="__acx-ripple" style="top: -103px; left: -45px; transform: translate(17px, -1px) scale(0.88156);"></div>
<div class="__acx-ripple" style="top: -101px; left: -62px; transform: translate(34px, -3px) scale(0.88156);"></div>
</material-ripple>
</tab-button>
Note that I want to locate tab-button which has aria-label = Offline Reporting. Here's what all I have tried:
element = driver.find_element_by_xpath("//tab-button[#aria-label='Offline Reporting']")
element = driver.find_element_by_tag("tab-button")
element = driver.find_element_by_tag_name("tab-button")
element = driver.find_element_by_css_selector("tab-button")
None of the above actually worked. Can you point out the right way to do the same? I have to locate the tab-button (Out of many) which has aria-label as Offline Reporting
Quick Edit
Note that I am using Selenium Web Driver for the same purpose and the page I am trying to load is an Angular page. From my understanding, the content is dynamically rendered here and so even though I can see it in element inspector, It's not getting located with find_element function.
Here is the snapshot of the same:
Note that I have tried everything that's mentioned in the current answers... that is waiting for the page to load completely and even if it loads completely I tried a delay of 50s as shown below:
WebDriverWait(driver, 50).until(EC.visibility_of_element_located((By.XPATH, "//tab-button[contains(#class, 'tab-button') and #aria-label='Offline Reporting']")))
But this ended up in timeout as well. The point I am trying to raise through these lines is that this is a dynamically rendered page and so to handle it there's some other middleware mechanicsm needed like scrapy-selenium
Try to wait for button to be become visible and clickable:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//tab-button[#aria-label='Offline Reporting']")))
button.click()
To locate the element with text as Offline Reporting you can use either of the following Locator Strategies:
Using css_selector:
element = driver.find_element_by_css_selector("tab-button.tab-button[aria-label='Offline Reporting']")
Using xpath:
element = driver.find_element_by_xpath("//tab-button[contains(#class, 'tab-button') and #aria-label='Offline Reporting']")
As the element is an Angular element, ideally to locate 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:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "tab-button.tab-button[aria-label='Offline Reporting']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//tab-button[contains(#class, 'tab-button') and #aria-label='Offline Reporting']")))
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 do I click on a drop down menu that is hidden using Selenium?

I'm trying to click on a drop down menu, but since it is hidden, I'm getting the error:
could not be scrolled into view
I've done some digging and I see that using some JavaScript could help, but I'm not sure how to implement that into my Python script.
<div class="MuiSelect-root MuiSelect-select MuiSelect-selectMenu MuiSelect-outlined MuiInputBase-input MuiOutlinedInput-input jss987" tabindex="0" role="button" aria-haspopup="listbox" aria-labelledby="input-label-idTeam1Desktop select-idTeam1Desktop" id="select-idTeam1Desktop"><span>​</span></div>
<input name="idTeam1Desktop" type="hidden" id="idTeam1Desktop" value="">
This is what I have so far:
driver = webdriver.Firefox(profile, options=options)
driver.get("https://tradenba.com/trade-machine")
element = driver.find_element_by_xpath("//*[#id='idTeam1Desktop']")
element.click()
To click on the drop down menu and select the menu item with text as MIL you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following xpath based Locator Strategies:
Using XPATH:
driver.get('https://tradenba.com/trade-machine')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='select-idTeam1Desktop']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='MuiListItemText-root MuiListItemText-inset']/span/div/p[text()='MIL']"))).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
Browser Snapshot:

How to get this element and click on it with selenium?

Trying to make my bot click on a submit button.
<div class="usertext-buttons">
<button type="submit" onclick="" class="save">save</button>
<button type="button" onclick="return cancel_usertext(this);" class="cancel" style="display:none">cancel</button>
<span class="status"></span></div>
I want to get the second row element with the type="submit"
driver.find_element_by_xpath doesn't work since the xpath is different for every post. What can I pull here that generally works?
To click on the element with text as save you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("button.save[type='submit'][onclick]").click()
Using xpath:
driver.find_element_by_xpath("//button[#class='save' and text()='save'][#type='submit' and #onclick]").click()
Ideally, to click on the element you have 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, "button.save[type='submit'][onclick]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='save' and text()='save'][#type='submit' and #onclick]"))).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
Try using css selector:
driver.find_element_by_css_selector('div.usertext-buttons > button[type=submit]').click()

Categories