Python : Clicking first google result using Selenium - python

I've tried almost every method of trying to click on the first result of google search.
I was trying to find the element using almost every queue i've found on the net, unfurntantlly no one of them succeed.
Please is there anyone who think he knows how can i click on it or even just getting the href as a text and i'll nevigate there by myself ...
Thanks a lot !

Related

Find Element in Selenium (Python) not possible due to multiple html tags

Hi :) This is my first time here and I am new to programming.
I am currently trying to automate some work-steps, using Selenium.
There was no problem, mainly using the find_element(By.ID,'') function and clicking stuff.
But now I cannot find any element that comes after the second "html" tag on the site (see screenshot)
I tried to google this "multiple html" problem, but all I found was people saying it is not possible to have multiple html tags. I basically don't know anything about html, but this site seems to have more than one - there are actually three. And anything after the first one cannot be subject to the find_element function. Please help me with this confusion.
These "multiple html" are due to the i frames in the html code. Each iframe has its own html code. If the selector you are using is meant to find something inside one of these iframes you have to "move" your driver inside the iframe. You can find an example in this other question

Enter isolation mode for selenium element using cdp

Currently, I have a code that uses remote selenium server to look for ads and save their images, or take screenshots in case their image isn't easily available. As of now I use:
iframes = list(driver.find_elements_by_tag_name('iframe'))
to obtain each iframe and then loop through them doing iframe.screenshot(filename). This works fine for the most part, but some pages take bad screenshots (they screenshot other aprts of the page or sometimes they have an element blocking the ad).
So what I'm trying to do is use the newly implemented chrome dev tools protocol on selenium to isolate each element, so it's the only visible element on the screen. This should be possible since I'm able enter chrome's dev tools, right click my desired element and "enter isolation mode". I just don't know where to begin with the code because the documentation is pretty messy and no one seems to have posted about this previously.
I've found these methods: https://chromedevtools.github.io/devtools-protocol/tot/Overlay/#method-setShowIsolatedElements
https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-createIsolatedWorld
but I'm not sure how to even test them.
If anyone's got any experience working wiht this or can think of a better solution, I'd greatly apreciate your help.

Clicking Javascript links using selenium webdriver in Python

I've been working on automations for a few months now, and for a recent project, I came across a Javascript link with href format as "javascript:srcUp" and no "htm" at the end. I've tried multiple ways of identifying this element, including css_selector, xpath, link_text, execute_script, etc. But none of the methods seem to work. With the link_text method, I was able to identify the element, but the click command didn't really do anything. Was wondering if anyone here has faced this issue, and if someone has, it would be great if you could share a possible solution.
Thanks

Selenium not detecting an element

So I'm coding this bot to join my school classes for me using selenium and I'm facing this issue where it goes to my conferences page and it waits for a join button to appear and press on it. At first, it wouldn't be able to find it so I scratched the idea of time and I made sure that when it opens the page the join button will be there so I did that and even put this line of code just to make sure
join=WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"//span[contains(text(),'Join')]")))
yet it still can't find the join button either press it even though it's visible by the eye I will link the page source under in a link if you can just tell me how to find it also I cant use the href method since it isn't stored like that it's just stored like this
https://i.stack.imgur.com/ldxY7.png
so I'm really confused it's almost like this is an invisible element its funny because even when I do get an error I use control F on inspect element to check if I used correct XPATH and I always find it so I don't see why selenium cant please help I think I just missed something I just don't know what.
Page Source
Check the presence of the element by looping and checking the presence_of_element_element_located multiple times with 2/3 seconds delay added between every consecutive check.
An analogy from python selenium-
for i in range(5):
l= driver.find_element_by_css_selector("#js-gdpr")
time.sleep(5)
OR
Iterate over all the elements under the parent div and verify it using unique text/color and access it.
I think you are trying to do Google meet automation. In that website the "join now" button is declared as a dynamic path so, you can use the library pyautogui
impor pyautogui as pg
for i in range(5):
pg.press('tab')
time.sleep(2)
pg.press('enter')
This code will help you to find the join now button and to click it.

selenium can not Submit an answer in zhihu.com

url:https://www.zhihu.com/question/305744720/answer/557418746
use selenium can not reply answer,only human
options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
browser = webdriver.Chrome(executable_path='.\chromedriver.exe',chrome_options=options)
button_li = self.browser.find_elements_by_class_name('Button--blue')
if len(button_li) > 2:
print(len(button_li))
button_ele = button_li[4]
button_ele.click()
time.sleep(random.uniform(0.5, 3))
browser.find_element_by_css_selector('div.AnswerForm-editor').click()
time.sleep(random.uniform(0.5, 2))
js="""
var div=document.getElementsByClassName('public-DraftStyleDefault-block')
var text =document.createTextNode("君");
div[0].firstChild.appendChild(text)
"""
self.browser.execute_script(js)
browser.find_element_by_css_selector('Button.Button.AnswerForm-submit').click()
Summary of problem:
My problem is that I wrote the content to the answer box successfully, but I was identified as a machine. After that, my actions on the page seemed to stop working. How can I avoid being identified as a machine so that I can still use selenium to select my element?
I'm not sure where you are trying to select the submit button, but the following selector worked for me:
browser.find_element_by_css_selector('Button.Button.AnswerForm-submit').click()
With respect to being detected as a 'machine', it's not easy to avoid that.
There are several different ways they can detect you.
That said, here's one thing I found that can avoid some of the Selenium detection attempts. One thing they look for document variables called $cdc_ and $wdc_ that selenium uses, and for Chrome it would be $cdc_. Here is what I suggest you try:
Download a hex editor if you don't already have one. I used one from here.
open your chromedriver.exe in the hex editor.
Use the Search functionality to find any instance of $cdc_ or $wdc_ and replace basically any other string ending in an underscore. Myself, I found just one instance of $cdc_, and it looked like this:
'$cdc_asdjflasutopfhvcZLmcfl_'
I simply replaced it with 'Random_'
Hopefully this works and now you can traverse the site unimpeded. If not, try some of the following; the only problem with this is that it might break your chromedriver file so that the tests no longer work. But it could be worth a try, and if it does break you can easily download a fresh one.
Search the document for any usage of the words 'selenium', 'webdriver', or 'chromedriver' and delete them. These is another way that a site can tell you are using selenium.
Let me know if any of this helps or you have any questions. It's hard for me to come up with a concrete answer because I don't know how exactly the site is detecting selenium.

Categories