Selenium Python Help Getting element of a form - python

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

Related

Python Selenium Finding Element With Dynamic URL and Name with Little HTML

Hey everyone! Today I am working with Python 3 and Selenium web driver. I am encountering a problem with finding by element. Here is the HTML:
<a _ngcontent-pxc-c302="" class="name ng-star-inserted" href="/person/20d4a795d3fb43bdbee7e480df27b05b">michele regina</a>
The goal is to click on the first name that appears in a listed column.
The name changes with every page refresh. This comes with two problems in that the link text is the name and it changes, and the link constantly changes as well for each different name except for the part that says /person/
I tried the following:
driver.find_element_by_css_selector("a.name ng-star-inserted").click()
driver.find_element_by_class_name("name ng-star-inserted").click()
Which resulted in an element that is not a clickable error.
I also tried xpath by copying and pasting the entire XPath copied from Google inspector... lol
driver.find_element_by_xpath('/html/body/app-root/app-container/div/div/tc-app-view/div/div/div/ng-component/mat-sidenav-container/mat-sidenav-content/div/div[2]/tc-person-table-container/tc-collapse-expand[1]/section/tc-person-table/table/tbody/tr[1]/td[2]/tc-person-table-cell/a').click()
and it kind of works sometimes, which is weird but I am sure there has to be a better way. Thank you so much in advance! Any help is appreciated! Also, I am very new to Python, I know zero HTML, and I am also newer to stack! Thanks!
From the given full xpath the a tag you are looking for is in table,section,div. Look for an id in those tags and trace the a.

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.

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

Find element by value Selenium/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()

Copy xpath by finding element by tag name

So i was just trying to see if i could automate myself logging into the nike website when i realised that the xpath they use for the input changes every time you reload the page.
The only way i managed to find the element was by searching for it by the tag name input . However, when i then try to send the keys to it it gives me the error saying "element not visible". My question is how would i send the keys to it or is there a way of finding the xpath of an element each time i run the code by finding the element by the tag name (input)
Here is a copy of my code:
driver.get("https://www.nike.com/gb/en_gb/p/activity/login")
time.sleep(3)
driver.find_element_by_tag_name("input").send_keys("test")
And the website of the form is :https://www.nike.com/gb/en_gb/p/activity/login
Thanks in advance guys :)
I am able to set the email and password with below code
driver.find_element_by_xpath("//input[#type='email']").send_keys("abcd")
driver.find_element_by_xpath("//input[#type='password']").send_keys("abcd")
You can also use cssSelector to find elements:
driver.findElement(By.cssSelector("input[name='emailAddress']")).send_keys("abcd");
driver.findElement(By.cssSelector("input[name='password']")).send_keys("abcd");
Using css-selector is faster than xpath and, in my opinion, it's also easier to declare and customize.

Categories

Resources