Automatic click with Selenium, using By.XPATH - python

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.

Related

How to extract the text from a weird element type using Selenium?

After I select a day with the pop-up date picker, the line is filled with the date like this:
I need this text. I tried using this:
iframe = driver.find_element_by_xpath(path)
date = iframe.text
Except that this code doesn't work because the text is not inside the element (I still don't know exactly why). I searched a bit and I found out that it's something called a ShadowRoot element and sometimes it's extractable but sometimes it's not.
This is what the element HTML looks like in Chrome dev tools:
And this is what it looks like in Safari. Note the difference:
Of course, I can't use this because it's not actually different from Chrome.
I need a way to get the date from this box. Any help will be highly appreciated!
Try this xpath:
driver.find_element_by_xpath(".//input[#id='appointments_consultate']//following::div").text
If this is not working, can you go a level up on your webelement in Chrome browser and share the screenshot

Regex for more results locator on google scholar

I would like to click on more results on google scholar from Python software. But I cannot find the correct XPATH. I have located the results button but not the link to the next site which seems to be in the span one level below as can be seen from the picture. I have tried "//button[#aria-label='Next']" and "//button[#aria-label='Next']\span" and "//button[#aria-label='Next']\span\span[1]". why does this not work?
url = "https://scholar.google.ch/scholar?hl=en&as_sdt=0%2C5&q=bla&btnG="
driver = webdriver.Chrome("~/chromedriver")
driver.get(url)
driver.find_element_by_xpath("//button[#aria-label='Next']").click()
I get the error, element not clickable because I cannot access the position of the real button.
Below a screenshot of the node structure in the html.
Using chrome Dev-Tools, you can select the element and click on Copy XPath. This will give you a selector that is guaranteed to work with that element. I've attached an image that displays this:
This is what it returned: //*[#id="gs_n"]/center/table/tbody/tr/td[12]/a/b
However, this can be further tuned. After some analysis, I found that //*[(#id = "gs_n")]//a//b works just as well.
Since you are using the chrome driver, these values should work fine since the DevTools is the one that generated it.
Edit
I think that we were referring to different selectors, thus creating a problem for you. Consider the screenshot:
The blue highlight represents the element that I used while the green highlight represents what I think you focused on. As a general note, try to right click on the specific element you want when using Inspect Element.

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

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()

How to select an HTML id in XPath in Python, using a wildcarded string?

For the moment, I'm looking to make a program for a repetitive action which we need to make something like 1000 times by hand otherwise.
This action is done throughout a web browser (I'm using Chrome). My actual issue is the XPATH selector is changing at every connection but only one number. So, I use the recognition on the webpage linked using Selenium and associated WebDrivers.
The fact is my code run sometimes when the selector has the right name.
Indeed, as the css selector is changing permanently, it happens that this is the right one !
So, after making a headless browser, login to the company webpage, I have to recognize then click on a specific object on the navigator :
The problematic code is the following:
wait.until(EC.presence_of_element_located((By.XPATH, '//*[#id="__xmlview0--settingsButton-img"]')))
OT = driver.find_element_by_xpath('//*[#id="__xmlview0--settingsButton-img"]')
OT.click()
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#__select1-label')))
driver.save_screenshot("screenshot.png")
I have an idea but I don't know how to do it: Is it possible to add a random number instead of the 0 in xmlview0, which is the number issue within the CSS selector ?
I'm not a Python veteran and I really don't want to do the job by hand.

Categories

Resources