python - Cannot get data from dropdown using Selenium - Stack Overflow

I've tried all logical divs to get the dropdown list, but could not make it '.click'.Th

I've tried all logical divs to get the dropdown list, but could not make it '.click'. The URL I am trying to get the information from is /. I don't get any mistakes but when the code is running Chrome opens the site and just closes without clicking the link to make dropdown visible, so I could scrape data from it.

from selenium import webdriver
from selenium.webdrivermon.by import By
import csv

driver = webdriver.Chrome()
url = f''
driver.get(url)

div = driver.find_element(By.ID, value='region-selected-value')
div.click()
#selector = div.find_element(By.TAG_NAME,value='select')

print(div.text)

I've tried all logical divs to get the dropdown list, but could not make it '.click'. The URL I am trying to get the information from is https://krisha.kz/. I don't get any mistakes but when the code is running Chrome opens the site and just closes without clicking the link to make dropdown visible, so I could scrape data from it.

from selenium import webdriver
from selenium.webdrivermon.by import By
import csv

driver = webdriver.Chrome()
url = f'https://krisha.kz'
driver.get(url)

div = driver.find_element(By.ID, value='region-selected-value')
div.click()
#selector = div.find_element(By.TAG_NAME,value='select')

print(div.text)
Share Improve this question edited Nov 19, 2024 at 9:28 jonrsharpe 122k30 gold badges268 silver badges476 bronze badges asked Nov 19, 2024 at 9:20 AlanaghAlanagh 2345 silver badges14 bronze badges 1
  • Your code looks just fine. Do you see any errors/exceptions in the console? Just apply a few minutes sleep at the end of your code like this time.sleep(20). You will notice that the dropdown is clicked and can see the dropdown window. – Shawn Commented Nov 19, 2024 at 9:33
Add a comment  | 

1 Answer 1

Reset to default 1

As I have mentioned in the comment, your code to click on that dropdown element is just fine. I have tested it and it clicks the dropdown element opening the dropdown options. You just need to put a time.sleep(20) at the end so that you see it before the browser closes down.

Having said that, as I understand you are looking to scrape all the dropdown values. If that is correct, see the code below:

from selenium import webdriver
from selenium.webdrivermon.by import By
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome()
url = f'https://krisha.kz'
driver.get(url)
select = Select(driver.find_element(By.NAME, "das[live.rooms]"))
options = select.options
for option in options:
    print(option.text)

Result:

1 - комн.
1-2 - комн.
2 - комн.
2-3 - комн.
3 - комн.
3-4 - комн.
4 - комн.
4-5 - комн.
5 и более комн.

Process finished with exit code 0

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745570043a4633626.html

相关推荐

  • python - Cannot get data from dropdown using Selenium - Stack Overflow

    I've tried all logical divs to get the dropdown list, but could not make it '.click'.Th

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信