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.
Related
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
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
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
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()
I am trying to click a 'next' button with Selenium in Python, which source code is:
<div class="form-group clearfix">
<button id="experiment-method-previous" type="button" class="btn btn-dark pull-left" ng-click="navigate('run',$event)">Previous</button>
<button id="experiment-method-next" type="button" class="btn btn-primary pull-right" ng-click="navigate('export',$event)">Next</button>
</div>
I use the line:
driver.find_element_by_id('experiment-method-next')
but I get this error:
Unable to locate element: [id="experiment-method-next"]
Same for
driver.find_element_by_class_name('btn btn-primary pull-right')
Unable to locate element: .btn btn-primary pull-right
Any thoughts?
As per the HTML you have shared to invoke click() on the element with text as Next you have to induce WebDriverWait for the desired element to be clickable and you can use either of the following solution:
CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-primary.pull-right#experiment-method-next"))).click()
XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-primary pull-right' and #id='experiment-method-next']"))).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 think your elememnt is inside frame/iframe. To be able to interact with elements inside frame/iframe you have to switch to it's content like this:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//path_to_frame")))
then you can locate your element and interact with it:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "experiment-method-next"))).click()
and when you are done with content inside frame, you have to switch back to default content:
driver.switch_to.default_content()
Note: you have to do some imports:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
PS: if your elements is not inside frame, then just use WebDriverWait:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "experiment-method-next"))).click()
this will wait at least 10 seconds until element will be clickable and the clicks on it. Hope this helps.
The solution I found is to kill first any previous windows my application opens and switch to the latest one:
current_window = driver.window_handles[1]
driver.switch_to_window(current_window)
Now, the Next Button can be clicked as follows:
driver.find_element_by_id('experiment-method-next').click()
Many thanks to the users Andrei Suvorkov and DebanjanB for their help.