I'm trying to check the checkbox using Selenium. This is the element of the checkbox.
<div class="ams-item-text ng-binding" ng-bind-html="amssh.create_label(item)" ng-click="toggle_check_node(item)" role="button" tabindex="0">all contract signed</div>
I'm directly copying the x-path of this element and wrote the below code:
browser.find_element_by_xpath('//*[#id="advancedcontents"]/div/div/div[2]/div/div[1]/div[1]/div/div/div[2]/div[2]/div/div[2]/div[3]/div[2]').click()
But it won't never click the check box that I wanted, but click the check box way below, which is with this:
<div class="ams-item-text ng-binding" ng-bind-html="amssh.create_label(item)" ng-click="toggle_check_node(item)" role="button" tabindex="0">future</div>
What could be the issues? I try the checkbox element, or the text element (also clickable) but both doesn't work.
To click on dynamic element induce WebDriverWait() and visibility_of_element_located() and following xpath option.
WebDriverWait(browser,10).until(EC.visibility_of_element_located((By.XPATH,"//div[#class='ams-item-text ng-binding' and text()='all contract signed']"))).click()
You need to import following libraries.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
Try below code:
wait = WebDriverWait(browser, 20)
wait.until(EC.element_to_be_clickable(By.XPATH, "//div[contains(.,'all contract signed')]")).click()
Note : Add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
"ams-item-text ng-binding"browser.find_element_by_xpath('//*[#id="advancedcontents"]/div/div/div[2]/div/div[1]/div[1]/div/div/div[2]/div[2]/div/div[2]/div[3]/div[2]').click()
Related
HTML snapshot of website:
The button is the rebounds button on 'https://app.prizepicks.com/board'. I've tried using the copy xPath feature, but that does not work.
Try to wait until the elements show up or use time.sleep()
Below xpath works for me
rebounds_btn = driver.find_element_by_xpath('//*[contains(text(), "Rebounds")]')
rebounds_btn.click()
Try the below,
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), 'Rebounds')]"))).click()
do not forget to import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
To click on the element with text as Rebounds you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following solution:
driver.get("https://app.prizepicks.com/board")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Rebounds']"))).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 click on the Apply button on linkedIn but, having a hard time in doing so. I tried to target the id but I noticed that the id always changes when the page reloads.
I've tried:
browser.find_element_by_xpath("//span[text() = 'Apply']").click() and browser.find_element_by_xpath("//button[#type = 'Apply']").click() but it doesn't work.
Use either of the xpath.
browser.find_element_by_xpath("//span[normalize-space(.)='Apply']").click()
OR
browser.find_element_by_xpath("//span[contains(.,'Apply')]").click()
UPADTE:
It seems synchronization issue induce WebDriverWait() and wait for element_to_be_clickable()
WebDriverWait(driver,15).until(EC.element_to_be_clickable((By.XPATH,"//span[normalize-space(.)='Apply']"))).click()
You need to import below libraries.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I would like to click button "ja ik ga akkoord" on url anwb.nl with python selenium chrome. I have copied the relative xpath but when is use it i keep getting NoSuchElementException. Also id, name, etc no luck
I start with:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
driver = webdriver.Chrome(options=options)
driver.get('https://anwb.nl')
When i inspect the page, xpath of the button gives me:
//*[#id="accept default level"]
When i use this with ...by_xpath i get NoSuchElementException
The code of the button is:
<button class="btn-decide_link-internal" type="button"
name="save"
id="accept default level"> ==$0
Ja, ik ga akkoord</button>
I tried id (accept def...), name (save), but all nosuchelement
In general i would really like to understand how to interpret the web code in general can solve future problems.
The element with text as Ja, ik ga akkoord is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://www.anwb.nl/");
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src*='anwb']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn-decide_link-internal"))).click()
Using XPATH:
driver.get("https://www.anwb.nl/");
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(#src, 'anwb')]")))
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[#id='accept default level']"))).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:
Here you can find a relevant discussion on Ways to deal with #document under iframe
There is an iframe.Induce WebDriverWait and switch to frame first and then click on the button.
EC.frame_to_be_available_and_switch_to_it()
EC.element_to_be_clickable()
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
driver = webdriver.Chrome(options=options)
driver.get('https://anwb.nl')
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME,"iframe")))
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"accept default level"))).click()
while learning how to use selenium, Im trying to click an element but nothing happens and Im unable to reach the next page. this is the relevant page: http://buyme.co.il and Im trying to click: הרשמה
I managed to print the desired element (הרשמה) so I guess Im reaching the correct place in the page. but 'click()' doesnt work.
the second span <span>הרשמה</span> is what i want to click:
<li data-ember-action="636">
<a>
<span class="seperator-link">כניסה</span>
<span>הרשמה</span>
</a>
</li>
for elem in driver.find_elements_by_xpath('//* [#id="ember591"]/div/ul[1]/li[3]/a/span[2]'):
print (elem.text)
elem.click()
also tried this:
driver.find_element_by_xpath('//*[#id="ember591"]/div/ul[1]/li[3]/a').click()
I expected to get to the "lightbox" which contain the registration fields.
Any thoughts on the best way to accomplish this?
Explicit Waits - An explicit wait is a code you define to wait for a certain condition to occur before proceeding further in the code.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome()
browser.get("https://buyme.co.il/")
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, 'ember591')))
elm = browser.find_elements_by_xpath('//div[#id="ember591"]/div/ul[1]/li[3]/a')
elm[0].click()
Update:
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'login')))
email = browser.find_elements_by_xpath("//form[#id='ember1005']/div[1]/label/input")
email[0].send_keys("abc#gmail.com")
password = browser.find_elements_by_xpath("//form[#id='ember1005']/div[2]/label/input")
password[0].send_keys("test1234567")
login = browser.find_elements_by_xpath('//form[#id="ember1005"]/button')
login[0].click()
The desired element is an Ember.js enabled element so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use the following Locator Strategy:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='הרשמה']"))).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 had to locate the iframe ,my html source code looks like
<iframe border="0" scrolling="yes" src="problem_list.do?sysparm_query=u_service_ticket_triage_group.name!%3Dxbt%20tech%20support%5Eu_program.name%3DTX_STAAR%5Eu_reject_ticket!%3Dtrue" name="TribName" width="100%" allowtransparency="true" style="width: 100% !important; height: 800px !important;" frameborder="0" id="TribID"></iframe>
I tried using id and name to locate this iframe but i got error
"no such element: Unable to locate element:"
Following are once that i tried
iframe = driver.find_element_by_name("TribName")
driver.switch_to.frame(iframe)
iframe = driver.find_element_by_id("TribID")
driver.switch_to.frame(iframe)
Could you suggest on how I can select this iframe.
As per the HTML you have provided to switch to the desired frame you need to use WebDriverWait for the frame to be available and then switch using either of the methods :
Through Frame ID :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"TribID")))
Through Frame Name :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"TribName")))
Iframe might be generate dynamically so you might need to explicitly wait for its appearance:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("TribID"))
In case of nested frames, e.g.
<iframe id="OuterFrame">
<iframe id="TribID"></iframe>
</iframe>
you should switch sequentially to each frame starting from the high level ancestor:
driver.switch_to.frame("OuterFrame")
driver.switch_to.frame("TribID")