I have an "onclick" element in a webpage whose HTML reads as follows:
Fastener
I want to search this element using the string "fastener" or "Fastener" using Python + Selenium. The number "3625" will change depending on previous inputs, and hence cannot be searched for.
I tried the following, but in vain:
br.find_element_by_css_selector("a[#onlick*='fastener']").click()
Please suggest ways to do this. Thank you!
P.S.: I am using Python 2.7, with Chrome WebDriver and Chrome v62.
To search the element with text as Fastener you can use either of the following options :
Through Fastener :
br.find_element_by_link_text("Fastener").click()
Using onclick through fastener (xpath):
br.find_element_by_xpath("//a[contains(#onclick,'fastener')]").click()
Using onclick through fastener (css_selector):
br.find_element_by_css_selector("a[onclick^='fastener']").click()
Use the following code for that:
br.find_element_by_link_text("Fastener").click()
Hope it helps you!
Using capybara-py:
page.click_link("Fastener")
Capybara is designed to provide this and many other similar helper methods, such as one might need to write acceptance tests from the perspective of end users:
page.fill_in("Street", value="123 Main St")
page.select("United States", field="Country")
page.choose("Expedited shipping")
page.click_button("Place order")
Use contains to search element
//a[contains (#onclick='fastener')]
OR
//a[contains(text(),"Fastener")]
Related
this the website I am dealing with https://www.bseindia.com/corporates/ann.html?curpg=1&annflag=1&dt=20211021&dur=P&dtto=20211027&cat=Insider%20Trading%20/%20SAST&scrip=&anntype=A
Here I am able to send the code for the "security name" but in-order to submit it I need to click the dropdown element that comes after giving the security name. How do I achieve this with selenium. I used the code below and its not working(StaleElementReferenceException)
security_name = driver.find_element_by_id("scripsearchtxtbx")
security_name.send_keys('INE350H01032')
sec_click = driver.find_element_by_xpath('//*[#id="ulSearchQuote2"]/li')
sec_click.click()
This can also be accomplished using the Keys Library within Selenium. Selenium Keys not only sends input statements like strings, but it can also send commands such as escape, tab, or in this case enter. Your updated code should look as follows:
security_name = driver.find_element_by_id("scripsearchtxtbx")
security_name.send_keys('INE350H01032')
security_name.send_keys(Keys.ENTER)
sec_click = driver.find_element_by_xpath('//*[#id="ulSearchQuote2"]/li')
sec_click.click()
These are called special keys. For more examples and more information on this, see this link
you can paly with Xpath:
security_name = driver.find_element_by_id("scripsearchtxtbx")
security_name.send_keys('INE350H01032')
sec_click = driver.find_element_by_xpath("//ul[#id='ulSearchQuote2']/li//strong[text()='INE350H01032']")
sec_click.click()
same -->("//ul[#id='ulSearchQuote2']//strong[text()='INE350H01032']")
you can check more about xpath here: xpath
I want to read out the text in this html element using selenium with python. I just can't find a way to find or select it without using the text (i don't want that because its content changes)
<div font-size="14px" color="text" class="sc-gtsrHT jFEWVt">0.101 ONE</div>
Do you have an idea how i could select it? The conventional ways listed in the documentation seem to not work for me. To be honest i'm not very good with html what doesn't make things any easier.
Thank you in advance
Try this :
element = browser.find_element_by_class_name('sc-gtsrHT jFEWVt').text
Or use a loop if you have several elements :
elements = browser.find_elements_by_class_name('sc-gtsrHT jFEWVt')
for e in elements:
print(e.text)
print(browser.find_element_by_xpath("//*[#class='sc-gtsrHT jFEWVt']").text)
You could simply grab it by class name. It's 2 class names so it would be like so. by_class_name only uses one.
If the class name isn't dynamic otherwise you'd have to right click and copy the xpath or find a unique identiftier.
Find by XPath as long as font size and color attribute are consistent. Be like,
//div[#font-size='14px' and #color='text' and starts-with(#class,'sc-')]
I guess the class name is random?
I have <script language = 'javascript'></script> in html file.
I used driver.find_elements_by_xpath("//script[#language]") but it's not working.
How can i fix it?
See if this works:
driver.find_element_by_tag_name("script")
I would try this:
driver.find_element_by_xpath("//script[#language='javascript']")
You are using find_elements which will return a list. for a single web element we want to use find_element.
if there's a single language attribute, you can probably try this :
script_web_element = driver.find_element_by_xpath("//script[#language]")
I am a newer in Selenium and use python to build it. Recently, I found a question which want to ask someone who can help me to figure it. The question is the Xpath I want to get is randomly, for example:
'//*[#id="wiki-edit-wikiEdit26"]/div/div/div/div[2]/a[1]'
'//*[#id="wiki-edit-wikiEdit27"]/div/div/div/div[2]/a[1]'
'//*[#id="wiki-edit-wikiEdit28"]/div/div/div/div[2]/a[1]'
These three xpath are used on the same button, but the number after wikiEdit will be changed every time. Therefore, are there any way which can help me to run my script more smoothly? Thank you very much!
Here is my python code:
broswer.find.element_by_xpath('//*[#id="wiki-edit-wikiEdit26"]/div/div/div/div[2]/a[1]') .click()
You can use matches in xpath to do this,
broswer.find.element_by_xpath("//*[matches(#id, '^(wiki-edit-wikiEdit)[0-9]')]/div/div/div/div[2]/a[1]") .click()
so basically that matches the id anything starting wiki-edit-wikiEdit followed by numbers form [0-9]
You just need to format your string..
import random
# Random range 1 - 100
x = random.randint(1,100)
broswer.find.element_by_xpath(f'//*[#id="wiki-edit-wikiEdit{x}"]/div/div/div/div[2]/a[1]') .click()
You can use starts-with or contains
broswer.find.element_by_xpath("//*[contains(#id,'wiki-edit-wikiEdit')]/div/div/div/div[2]/a[1]").click()
Also, using such long xpath's is not recommended. Use css-selectors over xpath.
http://www.m2sys.com/.
When I click on CloudApper™ under Solutions I get an ascii code error.
I find the element using:
driver.find_element_by_partial_link_text('CloudApper™')
How can I click the partial link?
Just ignore the TM in superscript while searching for CloudApper™.
driver.find_element_by_partial_link_text('CloudApper')
There are two instances by link text CloudApper™ in the target page. You can use this for finding the link cloudApper located below solutions. driver.find_element_by_xpath("//ul[#id='mobile-advanced']//a[contains(#href,'cloudapper')]")
CloudApper™ is a label, you have to click logo, use this xpath
//a[#href='http://www.m2sys.com/cloudapper-modularized-framework-enterprise-cloud-application/']
Try the following,
first, you need to interact with the menu:
driver.find_element_by_id("menu-item-15554").click();
then click on the actual element:
driver.find_element_by_xpath("//a[contains(#href, 'cloudapper-modularized-framework-enterprise-cloud-application')]").click();