Currently, I try to select a label by clicking on it but a problem the element has no ID:
<div class="cas__wayf-idp h5-like">
<span class="space space--sm"></span>
<input type="radio" name="selection" class="js-wayf-composant" id="idp-EDU" value="EDU" data-memorisable="true" checked="checked">
<label for="idp-EDU" class="form__label">TEXT IN THE LABEL</label>
</div>
I see that there are plenty of other ways including the xpath with text. After many tries I got this result:
driver.find_element("xpath", "//*[text()='TEXT IN THE LABEL']").click()
And this error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[text()='TEXT IN THE LABEL']' is not a valid XPath expression.
Hope you can help me, thanks.
You can use contains method to select text node
driver.find_element(By.XPATH, "//*[contains(text(),'TEXT IN THE LABEL')]").click()
#OR
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//*[contains(text(),"TEXT IN THE LABEL")]'))).click()
#Imports
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
Related
I have a html content like this
<div class="ng-star-inserted">
<span class="ng-star-inserted">
Seismic interpreter
</span>
<span class="ng-star-inserted">
Geophysicist
</span>
</div>
I have a selenium script where I am trying to get the first role name and second role name.
I tried this, but it's not giving me the 1st and 2nd elements as expected. Any idea what am I missing? thanks!
'//div[#class="ng-star-inserted"]//span//a[1]'
'//div[#class="ng-star-inserted"]//span//a[2]'
You can xpath indexing in this case :
so instead of this :
'//div[#class="ng-star-inserted"]//span//a[1]'
'//div[#class="ng-star-inserted"]//span//a[2]'
use this :
"(//div[#class='ng-star-inserted']//span//a)[1]"
"(//div[#class='ng-star-inserted']//span//a)[2]"
or if you think the text won't change, you can make use of LINK_TEXT as well or PARTIAL_LINK_TEXT
Sample code :-
wait = WebDriverWait(driver, 10)
button1 = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Seismic interpreter")))
button1.click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
So you can use the below way
((//a[contains(text(),'Seismic interpreter')])[2])
((//a[contains(text(),'Geophysicist')])[2])
clickLink=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"((//a[contains(text(),'Seismic interpreter')])[2])")))
checkbox.click()
It would be nice if someone knows how to select the checkbox using Selenium with Python.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
no_class = browser.find_element(By.XPATH, "//div[#id='icon-literary-collections']/following-sibling::a[1]")
no_class.click
and this is the HTML Part :
<div class="a-checkbox" style="">
<label for="checkbox-non--classifiable" style="">
<input id="checkbox-non--classifiable" type="checkbox" name="" value="" nodeid="non--classifiable" style="" class="">
<i class="a-icon a-icon-checkbox"></i>
<span class="a-label a-checkbox-label" style="">Non-Classifiable</span>
</label>
</div>
I want to select the checkbox "Non-classifiable" but i couldn't do that it tried I tried as id, name, link_text but could not detect what should be used?
You can try clicking on it by using its text in the xpath and using explicit wait so that the script waits till the element is present on the page.
You can do it like:
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Non-Classifiable']")))
element.click()
You need 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
WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "//span[#class='a-label a-checkbox-label']"))).click()
Javascript click:
checkBox=WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "//span[#class='a-label a-checkbox-label']")))
driver.execute_script("arguments[0].click();", checkBox)
Note : please 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
I'm trying to get Python / Selenium to properly click on a dropdown menu and select "IT" but the best I could do was find the element and get an error stating I couldn't input text on what I have found.
Basically, the user would click on that menu and select IT or type IT and press enter.
HTML code:
<div class="form-group">
<label class="col-md-3 control-label" for="id_tenant_group">Tenant group</label>
<div class="col-md-9">
<select name="tenant_group" nullable="true" class="netbox-select2-api form-control" data-url="/api/tenancy/tenant-groups/" data-filter-for-tenant="group_id" placeholder="None" id="id_tenant_group">
<option value="" selected>---------</option>
<option value="2">IT</option>
<option value="1">OT</option>
</select>
</div>
When I inspect the element, I can see there is a span element that triggers an event showing another span where finally my option can be seen.
I cannot select via visible text, because other menus also contain the same "---------" visible text.
I've captured a couple screenshots to illustrate the issue, hope that helps.
html code browser inspect
To be honest I am really lost, any suggestions will be much appreciated.
EDIT:
I've attempted the following:
tenant_g_element = Select(browser.find_element(By.XPATH, '//span[#id="select2-id_tenant_group-container"]'))
tenant_g_element.selectByVisibleText("IT")
But I've got the following error:
selenium.common.exceptions.UnexpectedTagNameException: Message: Select only works on <select> elements, not on span
Your first line is wrong. It should be something like this:
tenant_g_element = Select(browser.find_element(By.ID, 'id_tenant_group'))
because your has the attribute id="id_tenant_group". There is no need to use By.XPATH...if you have some reason to need that, then you'll need to look up how to specify an XPATH (note that //span is going to find a <span>, not a <select> for example).
This error message...
selenium.common.exceptions.UnexpectedTagNameException: Message: Select only works on elements, not on span
...implies that the element which you are passing to Select class is not a <select> node but a <span> element which isn't supported.
Seems your locator was identifying a <span> element instead of the <select> element. Hence you see the error. Additionally, selectByVisibleText() is not a valid Python method, instead you need to use select_by_visible_text().
Solution
To click on the option with text as IT, you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR and select_by_value():
tenant_g_element = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select.netbox-select2-api.form-control#id_tenant_group[name='tenant_group']"))))
tenant_g_element.select_by_value("2")
Using XPATH and select_by_visible_text():
tenant_g_element = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[#class='netbox-select2-api form-control' and #id='id_tenant_group'][#name='tenant_group']"))))
tenant_g_element.select_by_visible_text("IT")
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
from selenium.webdriver.support.ui import Select
Your Xpath seems wrong. You are trying to locate a span tag but you should locate select element. Additionally you should apply webdriver wait for of select element
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.webdriver.support.ui import Select
select = Select(WebDriverWait(driver, 50).until(
EC.presence_of_element_located((By.ID, "id_tenant_group"))))
WebDriverWait(driver, 50).until(
EC.presence_of_element_located((By.XPATH, "//select[#id='id_tenant_group']/option[2]")))
WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.XPATH, "//select[#id='id_tenant_group']//options[contains(.,'IT')]")))
select.select_by_visible_text('IT')
I am trying to enter some text into a text box on a webpage, here is the screenshot screenshot for Enter Symbol.
My code works fine for all other elements except for the highlighted text box in the screenshot:
WebDriverWait(driver, 40).until(EC.visibility_of_element_located((By.XPATH, '//*[#id="main-content-column"]/div/div[3]/div/div[1]/form/div/div/div[2]/input'))).send_keys("aapl")
It doesn't look like in a frame or iframe. Please see the source code for the website:
<input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" placeholder="Add a symbol..." class="sec-search-box bc-form__add-symbol-input ng-pristine ng-invalid ng-invalid-required ng-touched placeholder" data-ng-model="userEnteredSymbols" data-barchart-clear-input="" required="">
Thanks a lot.
You can input the value using the xpath:
WebDriverWait(driver, 40).until(EC.visibility_of_element_located((By.XPATH, "//input[#data-ng-model='userEnteredSymbols']"))).send_keys("aapl")
You are using Absolute xpath which is not a recommended way. You can try with below xpath with webdriver wait
element = WebDriverWait(browser, 30).until(EC.visibility_of_element_located((By.XPATH, "//input[#data-ng-model= 'userEnteredSymbols']")))
element.send_keys("aapl")
you need below import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I'm trying to click on this button to go to the next page, but for some reason, I'm unable to. I tried xpath, css, and class selectors as well as the data-trekkie-id attribute, but nothing I have tried worked. Any help? Code below:
<div class="step__footer" data-step-footer="">
<button name="button" type="submit" class="step__footer__continue-btn btn " data-trekkie-id="continue_to_shipping_method_button" aria-busy="false">
<span class="btn__content">
Continue to shipping method
</span>
</button>
</div>
As per the HTML to invoke click() method on the button with text as Continue to shipping method you need to induce WebDriverWait 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.step__footer__continue-btn.btn[data-trekkie-id='continue_to_shipping_method_button']>span.btn__content"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='step__footer__continue-btn btn' and #data-trekkie-id='continue_to_shipping_method_button']/span[#class='btn__content']"))).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
This should work:
driver.find_element_by_xpath("//*[contains(local-name(), 'button') and contains(#class, 'step__footer__continue-btn')]").click()