I am trying to reach an web element in Selenium in Python 2.7. The element ID is like this:
cell.line.order(240686080).item(250444868).unitCost
The first number string '240686080' is known, but the second number '250444868' is unknown beforehand.
I tried to reach it by something like this.
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("somewebsite.com")
driver.find_elements_by_id('input.line.order(240417939).item('+ '\d{9}'+').unitCost')
So my questions is, is there anyway we can search and reach this element only with part of the ID known?
I found similar question answered below, but it was in C#. And unfortunately, I don't know C#.
Finding an element by partial id with Selenium in C#
Thank you in advance!
Edited 4/8
The element is coded like this:
<td id="cell.line.order(240417939).item(250159165).unitCost" class="or_monetarydata">
<input id="input.line.order(240417939).item(250159165).unitCost" type="hidden" value="135.00"
name="order(240417939).item(250159165).unitcost"></input>
135.00
</td>
I could get the element list by
value=driver.find_elements_by_xpath("//*[contains(#id, 'input.line.order(240417939)')]")
Thank you!
Although the answer is in C#, the underlying solution is still the same, by using CSS selectors:
driver.find_elements_by_css_selector('input[id*='cell.line.order(240686080)']')
or XPath will also be able to do this:
driver.find_elements_by_xpath('//*[contains(#id, 'cell.line.order(240686080)')]')
Related
I'm creating an auto-login Google Calendar bot and I've run into an issue where the xpath for a certain element keeps changing every time I login(but it has a similar form). Here are some examples:
//[#id="c307"]/div/div[1]/div/div[2]/div/div[1]/div[1]/div/label/div[1]/div/input
//[#id="c236"]/div/div[2]/div[1]/div/div/span/div/div[1]/div[2]/div[1]/div/div[1]/input'
//*[#id="c27"]/div/div[1]/div/div[2]/div/div[1]/div[1]/div/label/div[1]/div/input
and etc.
How can I use Selenium to click on this dynamic element given that it always changes? Help will be much appreciated, thank you.
I would recommend using a querySelector over an xpath
MDN web docs document.querySelector
An implementation of this would look like:
driver.execute_script('document.querySelector("#specific_element_id").click()')
This code finds an element that looks something like <input type="button" id="specific_element_id"> and will click it.
I am trying to extract an attribute from a deep <div> tree. I tried find_all and select; both failed. Please help. The line in grey is what I need. I need to get the value of data-num out which is 2.
Basically, I need to get the value orange on the top right of https://www.xin.com/c2b_car_o/201/
Thank you for your help!
thank you for the help. I finally found a solution.
[problem]: I could not locate the web elements using beautifulsoup.
[reason]: the element was rendered by javascript. I don't know why? but that's the answer I got from another source. I was told that I need to use selenium.
[solution]: I used selemium to successfully extracted the numbers. See my code below.
[more help]: the problem is solved! But I am unclear what situation is good for selenium. And how do I know what a specific element is rendered by javascript which can't be extracted using beautifulsoup. Please give a try and post your code if you can do it using bs4. Or, please provide more explanations. Thanks.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('G:\chromedriver\chromedriver')
driver.get("https://www.xin.com/c2b_car_o/201/")
elements = driver.find_elements_by_xpath('//*[#class="mt-number-animate-dom"]')
num_str=[el.get_attribute("data-num") for el in elements]
print(int(''.join(num_str)))
I am trying to make a basic form filler using Selenium on nike.com. I have completed most of it but I am having trouble clicking on the button to select the gender. I have attempted to use many examples of find_element_by_xxxxx code bit none of it has worked. Finding elements by id and xpath haven't come to much either. A typical error i get is Message: no such element: Unable to locate element. I am very new to coding so I could very easily have made an error, but any idea on how you guys would solve it would be much appreciated
That XPATH is very long and you can simplify.
By the looks of it , I would guess those Ids are changing every time there is a new session.
A more straightforward XPATH selector could be...
"//span[text() = 'Male']"
// specifies to search the entire document
span specifies the type of element to search for
text() specifies text that needs to be inside the element
(this will give you the span element but it should still work)
or
"//span[text() = 'Male']/parent::li//input"
(this will give you the actual input button)
Also , like Ollin Boer Bohan suggested, look into using waits before performing actions on your elements.
#cavan answer is correct, Also you can use xpaht like this too
//input[#type='button']//following::span[text()='Male']
Here we can use following to locate the male, same you can do for female button
I am trying to get the xpath for the following code but can't seem to figure it out.
<i class="icon icon-button-follow pointer action-button js-follow-unfollow-button" data-type="follow" data-id="3470861"></i>
You have to understand: what does xpath is? Which goal it has? Xpath is a locator, which you can to use to separate one elements and another one.
I can recommend to you this http://practicalsqa.net/xpath-brainteasers-and-exercises/ class to better understanding this question.
In you case xpath may be for ex.:
//i[#class="icon icon-button-follow pointer action-button js-follow-unfollow-button"]
or
//i[#data-type="follow"]
You have to use something what is unique for this element.
I depends why you want that node, right?
//i[#data-id="3470861"]
should find it. But will you always have that id?
I want to send 'username' using Selenium python, for the following html:
<div class="entryforms-elements">
<input id="entryforms-control-element" class="entryforms-element-text" type="text" autocomplete="off"></input>
</div>
But none of the following method worked:
browser.find_element_by_xpath('//input[#id="entryforms-control-element"]').send_keys('username')
browser.find_element_by_id('entryforms-control-element').send_keys('username')
browser.find_element_by_class_name('entryforms-element-text').send_keys('username')
Please help me!
You might want to check the size of the found element to be sure you are actually locating something.
If you are using firefox this might be getting in the way:
selenium webdriver is clearing out fields after sendKeys had previously populated them
Or some javascript might be munging the field after your keys are sent (maybe some js firing after page load).
You also might try calling click() first and then sending the keypresses -- perhaps that will help focus the element if it's the issue mentioned above or something similar.
Are you sure that the select by Xpatch is correct?
in my opinion should be something like that:
browser.find_element_by_xpath(".//*[#id='entryforms-control-element']")
in every case just to be sure is better you clean and then you introduce your input
browser.find_element_by_xpath(".//*[#id='entryforms-control-element']").clear()
browser.find_element_by_xpath(".//*[#id='entryforms-control-element']").send_keys('username')
in any case I advice you to use Firefox with the plugin firebug and firepath to know for sure the correct xpath of the elements
Jump on your browser developer tools and check how long it takes the element to load in the network tab. Then set a wait in your code between the load and element find based on this information.