Find element by value Selenium/Python - python

I am using Selenium with Python to automaticlly extract some data from our power plants and right now I need to click on an element.
The problem is that the element's xpaths and order change for each plant we are monitoring. The only static info is the value, just like in the 3rd line value="T_U0.
I tried many approaches and I couldn't find a solution. I can't use index or child because the order of the parameters is changing. I tried CSS selector with no success.
Here you get some of my tries...
driver.find_element_by_xpath("//input[#value='T_U0']").click()
driver.find_element_by_css_selector("input[#data-id-sys-abbreviation='388']").click()
I tried many other things but I was just desperately trying anything.
What I really need is a find_by_value, if there is a way of doing it please let me know, if there isn't please show me how I can do it.
I need to click in some options that change order accordingly to the plant

The problem is with the first xpath. You are trying to locate an input while you need to get option.
Try this:
driver.find_element_by_xpath("//option[#value='T_U0']").click()

You can try to click/select element via displayed text.
Pseudo code:
driver.find_element_by_xpath("//option[text()="Some text"]").click()

Related

Automatic click with Selenium, using By.XPATH

Do you see this little blue logo in the image below? Using WebDriverWait I would like to automatically click on this blue logo, in order to open the list of those who have left the likes.
I used By.XPATH., More precisely: //*[#id="jsc_c_z"]/span[1]/span/span/div , but it doesn't work. NOTA :I've noticed that the IDs and ranges on this page look suspiciously obfuscated and so maybe they won't necessarily be the same every time. So maybe I don't think id = "jsc_c_z" that will be reliable. You may need to resort to using aria-labeld of the attribute on the target element div.
My code is:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="jsc_c_z"]/span[1]/span/span/div'))).click()
This instead is the link I would like to open https://www.facebook.com/FranzKafkaAuthor/posts/3985338151528881.
IMPORTANT: Maybe you have to enter with the Facebook login, but who does not want to do so will post a screenshot
PART 1 (UPLOADED)
PART 2
I hope someone can help me. Thanks
Can you try this?
//span[#data-hover='tooltip']/span
When I was checking this, I couldn't find the div tag you mentioned.

Python Selenium how to find element with changing text

I am working on a program that auto-completes questions. But the answers to the questions are choices and they will change every time.
There are four choices and I only want it to click one of them.
This work but the text will change every time: driver.find_element_by_xpath("//p[contains(text(),'The home of a person/ company.')]").click()
I have tried this: driver.find_element_by_class_name("choices").click() and it won't work.
So, I want no matter what the text part is, the program can search and click on it. But only clicking one of the choices.
Picture of the website with html
HTML:<p class="choices" xpath="1">The home of a person/ company.</p>
xpath of this://p[contains(text(),'The home of a person/ company.')]
absXpath:/html[1]/body[1]/div[2]/div[1]/div[2]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[2]/div[1]/div[3]/div[1]/div[3]/div[1]/label[4]/p[1]
Please help.
first check wheather you are using correct xpath because the xpath changes as the position of options are changed. Also, according to your code you can use specific id for this problem because class can be provieded for multiple elements but id is unique.
To click the first option use css selector:
driver.find_element_by_css_selector(".tbtow.exercise_frame>.each_question.mc_question>label:nth-child(1)>p")
Check classes spelling by yourself because I typed them from the picture you provided. Also, experiment with :nth-of-type(1) instead of :nth-child(1)
To change position use :nth-of-type(2), :nth-of-type(3) and so on.

Dynamic elements with Selenium and Python

I'm trying to fill a small form with Selenium, but it has dynamic elements that it doesn't let me capture in any way. In this link you can see the form: https://www.asefasalud.es/
I have tried almost all the search elements, to capture the object, but none of them work.
driver.find_element_by_xpath("//*[contains(#id, 'inputDiafnac')]").get(0).send_keys("2")
driver.find_element_by_xpath("//*[contains(#id, 'inputDiafnac')]").send_keys("2")
driver.find_element_by_xpath("//input[#id='inputDiafnac1']").send_keys("2")
driver.find_element_by_css_selector("#inputDiafnac1").send_keys("2")
driver.find_element_by_id("inputDiafnac1").send_keys("2")
I don't know if there is another way to capture these elements, thank you.
I found something I think might work.
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, "[name='calcular-seguro-medico']"))
driver.find_element_by_id("inputDiafnac1").send_keys("2")
Once you are done with elements inside this frame, you will need to switch back to the top level frame using
driver.switch_to.default_content()

Can't find twitter DM textbox by xpath using selenium

I am trying to find the textbox element using the find_element_by_xpath() method, but It keeps telling me it cant find said element, here's the line of code that does that.
I've tried finding it by link_text, partial link text, selector and it just doesn't work
bar = nav.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div/aside/div[2]/div[2]/div/div/div/div/div[1]/div/div/div/div[2]/div/div/div/div')
Thanks in advance!
So, I suggest creating your xpath if you want to be precise and avoid taking it based on html structure (which can change).
The locators looks like:
And you can take it with xpath:
//input[#placeholder='Search people' and #role='combobox']
To avoid this problem, I suggest going trough a tutorial for a better understanding regarding how to create custom locators: Xpath tutorial
What was happening is: When I opened the tab to inspect the element, the DM structure changed because of my screen size so the xpath wasn't the same

Selenium Python Help Getting element of a form

Iv been trying to get the form elements on this website and for some reason I cant get them. Iv tried every way I can think of. I would really appreciate some help.
Here is my latest attempt
inputs = driver.find_elements_by_xpath("//input[#type='text']")
Here is the website.
Thanks in advance.
Edit : Github link for project
Edit : Ok so I found out that if you go straigt to website that the btn points to then it works. So I am assuming that slenium does not automaticly update to the HTML of the current page. How does one do this?
if you are getting 'NoSuchElementException', try using conditional wait before you do find_element. When you are loading another page wait until the element is visible and can be interacted.
Guide for that is here
check if the element is inside in iform, if so use
driver.switchTo().frame("framename")
EDIT
Focus on something unique in the tag. For your example use the class name used in the div. Do not use the ID tag identifier as it changes over time.
//*[contains(#class, 'ginput_container ginput_container_text')]/following::*[contains(text(),'Email')]/following::div[1]/descendant::input

Categories

Resources